开发者

.val() doesn't set value for select (jQuery)

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

开发者_高级运维JSFiddle here.

What should I change?

Thanks!


Put opt2 in quotes for val(). It's coming up as an error because it is undefined.


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

You asked for some other methods, but I only know of one other. This method doesn't require jQuery to function (note that the [0] grabs the normal Object rather than an jQuery wrapped 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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜