.val()没有为select(jQuery)设置值

时间:2010-09-05 03:52:12

标签: javascript jquery forms select

$('.reset').click(function () {
    $('select[name=one]').val(opt2);
});​

JSFiddle here

我应该改变什么?

谢谢!

1 个答案:

答案 0 :(得分:4)

opt2放入val()的引号中。它出现了错误,因为它是undefined


$('.reset').click(function () {
    $('select[name=one]').val('opt2');
});​

你问过其他一些方法,但我只知道另一种方法。此方法不需要jQuery运行(请注意[0]抓取正常Object而不是jQuery包裹Object。)

//Without jQuery... pretty sure I'm correct with the second method, but I'm rusty on that one.
document.getElementById('formID').one.selectedIndex = 0; // Sets the select back to the first option.
document.formName.one.selectedIndex = 0;

//With jQuery
$('select[name=one]')[0].selectedIndex = 0;