下拉列表不保留所选值



该值不保留,并返回到选择后的第一个值

这是我的jQuery
<script>
    if($("#ddlGender").val("Female")) {
        $('select[name^="ddlGender"] option[value="Female"]').attr("selected","selected");
    } else if($("#ddlGender").val("Male")) {
        $('select[name^="ddlGender"] option[value="Male"]').attr("selected","selected");
    } else {
        $('select[name^="ddlGender"] option[value=""]').attr("selected","selected");
    }   
</script>

这是我的下拉列表

<select name="ddlGender" id="ddlGender" onchange="${remoteFunction(action: 'index', controller: 'Main', params:''id=' + escape(this.value)', update: [success: 'd1', failure: 'ohno'], options: '[asynchronous: false]')}">
  <option value="">--Select--</option>
  <option value="Female">Female</option>
  <option value="Male">Male</option>
</select>

我不完全理解你为什么做select[name^="ddlGender"],因为简单的#ddlGender可以给你带来同样的结果。然而,在这里,我理解你的代码解决方案:

<select name="ddlGender" id="ddlGender">
    <option value="">--Select--</option>
    <option value="Female">Female</option>
    <option value="Male">Male</option>
</select>
$('#ddlGender').on('change', function () {
    if ($("#ddlGender option:selected").val()==='Female') {
        $('#ddlGender option[value="Female"]').attr("selected", "selected");
    } else if ($("#ddlGender option:selected").val()==='Male') {
        $('#ddlGender option[value="Male"]').attr("selected", "selected");
    } else {
        $('#ddlGender option[value=""]').attr("selected", "selected");
    }
});

JSFIDDLER

相关内容

  • 没有找到相关文章

最新更新