使用jQuery设置和获取select的值

时间:2015-04-14 23:10:56

标签: javascript jquery

我有一个关于使用jQuery设置select下拉值的问题

我已经写了这段代码

// This should find option I want and set it as selected
jQuery('[name=' + obj.name + ']').val(obj.value)
     .find("option[value=" + obj.value + "]").attr('selected', true);

// This should set value of select to the same as above
jQuery('[name=' + obj.name + ']').val(obj.value);

假设obj.name = "selManufacturer"obj.value是以下选项的任何选项值,我有这个问题:

第一段代码似乎有效。我可以看到我的选项已被选中。但是当我想获得.val() select时,我得到一个空字符串。知道为什么吗?

<select style="display:none;" class="js-ManufacturerList" name="selManufacturer" id="ManufacturerList">
    <option value=""></option>
    <option selected="selected" value="Apple">Apple</option>
    <option value="Microsoft">Microsoft</option>
    <option value="HP">HP</option>
</select>

当我这样做时

jQuery("#ManufacturerList").val() // ""
jQuery("#ManufacturerList option:selected").val() // ""
jQuery("#ManufacturerList").find(":selected").text() // ""

这只发生在Firefox

0 个答案:

没有答案
相关问题