How to prevent AutoPostBack when DropDownlist is selected using jQuery

后端 未结 4 1223
面向向阳花
面向向阳花 2021-01-22 19:32

I want to show a confirm dialog when the user selects an item in a DropDownList. If the user presses \"Cancel\", I want to stop the postback. Here is the function I add to the o

4条回答
  •  醉酒成梦
    2021-01-22 19:55

    After reading this solution, I just removed the AutoPostBack on my DropDownLists and used this because it was so simple:

    $("#DropDownList1").change(function (e) {
        if ($(this).val() == "0") {
            //do nothing
        }
        else {
            //do postback
            setTimeout("__doPostBack('DropDownList1','')", 0);
        }
    });
    

    or if you have multiple DropDownLists you want to make AutoPostBack:

    $("select").change(function (e) {
        if ($(this).val() == "0") {
            //do nothing
        }
        else {
            //do postback
            setTimeout("__doPostBack('" + this.id + "','')", 0);
        }
    });
    

提交回复
热议问题