修改jquery代码

修改jquery代码,jquery,Jquery,我有下一个jquery代码,我想用name=“更改value=”“。怎么做 state.find("option[**value**='" + self.**val()** + "']").show(); jQuery("#city").on("change", function () { var localitate = jQuery("#state"); var self = jQuery(this); state.find("opt

我有下一个jquery代码,我想用
name=“
更改
value=”“
。怎么做

state.find("option[**value**='" + self.**val()** + "']").show();

    jQuery("#city").on("change", function () {
        var localitate = jQuery("#state");
        var self = jQuery(this);
        state.find("option").hide();
        state.find("option[value='" + self.val() + "']").show();
    }).change();
更新:

我有

<select name="city" id="city">
<option value="ohio" name="1">Ohio</option>
<option value="berna" name="2">Berna</option>
</select>

<select name="state" id="state">
<option name="1">State1</option>
<option name="1">State2</option>
<option name="2">State1</option>
<option name="2">State2</option>
</select>

俄亥俄州
伯纳
状态1
状态2
状态1
状态2
我希望使用名称而不是值来显示第二个选择选项取决于第一个选择选项,请尝试:

state.find('option[name="' + self.attr("name") + '"]').show();

只需使用以下内容:

jQuery("#city").on("change", function () {
    var state = jQuery("#state");
    state.val("") //Clear the previous selection
    var self = $(this).find("option:selected") //This will be the select so we need to get the selected option from this
    state.find("option").hide();
    state.find("option[name='" + self.attr("name") + "']").show(); //Search them by the name and then show them
}).change();

不确定您想做什么您是什么意思?似乎您只想将
更改为
名称
。是否根据第一次选择的值隐藏第二次选择的元素?这就是你想要的?好吧我回答了,我希望这就是你想要实现的。第一个选项有Sibiu这样的值,第二个select有Craiova这样的值。关系将如何建立。例如,如果我在第一个选项中选择一个选项,那么第二个选项上的选项将如何被过滤。如果我选择Sibiu,我将得到名称41,但没有选项第二个选择中的值为41,第二个选择中只有1和2存在值41。刷新后在firefox中工作,但在chrome中,第二次选择的所有内容都是隐藏的。更改脚本在哪里?我在代码中没有更改任何内容。在chrome中,仅显示值=1