JQuery problems when setting dropdown list value
I am running into a strange issue. I have a dropdown list that contains some values. Using JQuery code I am trying to set one option as the selected one:
$('.myform #company').val('10K');
The company element is the dropdown select and one of the options in it has the value of 10K. If I try the same code for a different value in the same dropdown it works just fine.
I'm a little bit at a loss. I have a hard time thinking that the actually string value 10K is what is causing it...
Thanks
<option value="">Select</option>
<option value="10K ">10k Filing</option>
<option value="501C">501c3 Filings</option>
<option value="F开发者_JAVA技巧990">Form 990</option>
Surprised nobody has pointed this out, but the value of that option has an extra space:
<option value="10K ">10k Filing</option>
Can you list the <option>
values for this drop down?
Btw your selector only needs to be $('#company')
if($('#company').length){
for (i = 0; i < document.getElementById('#company').options.length; i++) {
if (document.getElementById('#company').options[i].value).toLowerCase() == '10k' {
document.getElementById('#company').selectedIndex = i;
break;
}
}
}
I find it easier to use pure javascript in this scenario... There are jQuery plugins that help with select box manipulation, but even I as a big jQuery fan think it's very unwise to do that when you can achive these task with pure javascript more efficiently and without the need to load extra libraries.
well javascript can perform xml transforms so you could write one to do this for you. I'm a little rusty on my xslt but it would look something like this:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<Select>
<xsl:template match="/">
<xsl:for-each select="option">
<option value="<xsl:value-of select="catalog/cd/title"/>"><xsl:value-of select="catalog/cd/title"/> Filling</option>
</xsl:for-each>
</xsl:template>
</Select>
</xsl:stylesheet>
So this is about what you need, you'll probably need to work on it a little more but you get the idea
精彩评论