0

我的 mvc4 razor 页面上有一个复选框和一个下拉列表

当我取消选中我的复选框时,我需要将下拉列表的选择重置为 0

$("#chkProcess").click(function (event) {
        //on-click code goes in here.
        var chkProcess = $(this);
        if (chkProcess.is(':checked')) {
           //do process
        } else {
            $('#locationSelectList').val('0');
            alert("" + $("#locationSelectList option:selected").val());

        }
    });

当我取消选中我的复选框时,我收到显示选定索引 = 0 的警报,但在屏幕上它实际上并没有反映更改。但我也需要下拉菜单来重置屏幕上的选择。为什么我会看到这种行为,我在这里做错了什么?

我的 _layout 页面上引用了这些 Jquery 文件

    <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery-ui-1.8.11.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.treeview.js")" type="text/javascript"></script>
     <script src="@Url.Content("~/Scripts/jquery.ui.selectmenu.js")" type="text/javascript"></script>

    <script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>

我尝试了所有这些

$('#locationSelectList').attr('selectedIndex', '0');
 $('#locationSelectList').val('0');

但我可以在警报消息中看到它正在重置选定的索引,但在页面上没有得到相同的反映。

4

3 回答 3

4

看看这个链接。

http://jsfiddle.net/sheeban/wGAMW/2/

HTML

<input type="checkbox" id="chkProcess"/>
<select id="locationSelectList">
    <option value="chennai">chennai</option>
    <option value="bangalore">bangalore</option>
</select>

JS

$(function() {
$("#chkProcess").click(function (event) {
    if(!$(this).is(':checked')) { 
        $('#locationSelectList').prop('selectedIndex',0);
    } 
 });
});
于 2013-09-04T18:13:11.690 回答
1

选择第一个选项(默认选择选项)?

尝试这个 :

document.getElementById("<%#mydropdownlist.ClientID%>").value = 0;

另一种方法使用Jquery

$check .click(function() {

    $ddlHeader.find('option:first').prop('selected', 'selected');

});

我有一个FIDDLE给你

于 2013-09-04T17:55:43.670 回答
0

使用 。道具功能:

$("#chk").on("click", function()
{
    if($(this).prop("checked"))
    {
        $('#locationSelectList').val('0');
    }
});

http://jsfiddle.net/hescano/BDh6X/1/

于 2013-09-04T18:02:45.733 回答