修改jquery代码

时间:2014-03-20 14:50:46

标签: jquery

我有下一个jquery代码,我想要value=""更改name=""。怎么样?

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>

我希望使用名称而不是显示第二选择选项的值取决于第一个选择选项

2 个答案:

答案 0 :(得分:0)

尝试:

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

答案 1 :(得分:0)

只需使用:

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();

Fiddle