多个 jquery 命令不起作用

Multiple jquery command won't work

本文关键字:不起作用 命令 jquery 多个      更新时间:2023-09-26

只有第一个使用 id "exp_month" 的命令有效,其他 2 个不起作用。id当前在html中设置。

    if (month!="")
            $('select#exp_month>option:eq('+month+')').attr('selected', true);
    if (year!="")
            $('select#exp_year>option:eq('+year+')').attr('selected', true);
    if (state!="")  {
            $('select#x_state>option:eq("'+state+'")').attr('selected', true);
    }

问题是"option:eq"按索引值而不是按选项值选择选项。

我现在使用:

$('#id").val(theVal)