2

我有一个带有 AutoPostback 的单选按钮列表。单击特定项目时,我想停止回发并显示一个窗口。

如果 autopostback=false,我可以停止传播并显示一个窗口,所以我唯一需要的是能够停止回发。

我试图阻止回发,但没有运气。因此:关于如何停止回发的任何想法?我尝试了 preventDefault()、e.stopImmediatePropagation()、e.stopPropagation() 并返回 false。

我的单选按钮列表:

 <asp:RadioButtonList ID="rblShippingMethod" runat="server" AutoPostBack="True" AppendDataBoundItems="true" />

呈现如下:

 <table id="ctl00_ctl00_plhBody_plhMain_rblShippingMethod" border="0">
<tr>
    <td><input id="ctl00_ctl00_plhBody_plhMain_rblShippingMethod_0" type="radio" name="ctl00$ctl00$plhBody$plhMain$rblShippingMethod" value="1,1" onclick="javascript:setTimeout('__doPostBack(\'ctl00$ctl00$plhBody$plhMain$rblShippingMethod$0\',\'\')', 0)" /><label for="ctl00_ctl00_plhBody_plhMain_rblShippingMethod_0">text</label></td>
</tr><tr>
    <td><input id="ctl00_ctl00_plhBody_plhMain_rblShippingMethod_1" type="radio" name="ctl00$ctl00$plhBody$plhMain$rblShippingMethod" value="1,2" onclick="javascript:setTimeout('__doPostBack(\'ctl00$ctl00$plhBody$plhMain$rblShippingMethod$1\',\'\')', 0)" /><label for="ctl00_ctl00_plhBody_plhMain_rblShippingMethod_1">text</label></td>
</tr><tr>
    <td><input id="ctl00_ctl00_plhBody_plhMain_rblShippingMethod_2" type="radio" name="ctl00$ctl00$plhBody$plhMain$rblShippingMethod" value="1,3"  /><label for="ctl00_ctl00_plhBody_plhMain_rblShippingMethod_2">text<br/> <span class="deliveryOption">text</label></td>
</tr><tr>
    <td><input id="ctl00_ctl00_plhBody_plhMain_rblShippingMethod_3" type="radio" name="ctl00$ctl00$plhBody$plhMain$rblShippingMethod" value="2" onclick="javascript:setTimeout('__doPostBack(\'ctl00$ctl00$plhBody$plhMain$rblShippingMethod$3\',\'\')', 0)" /><label for="ctl00_ctl00_plhBody_plhMain_rblShippingMethod_3">text</label></td>
</tr>

我有以下 JavaScript / jQuery:

<script type="text/javascript">
    $(document).ready(function () {
        var id = $('input[value="1,3"]').attr('id');
        var combined = '#' + id;

        $(combined).click(function (e) {

            e.preventDefault();
            e.stopImmediatePropagation();
            e.stopPropagation();
            magicMethod('mylink');
            alert('Confirming I am even called');
            return false;
        });
    });
</script>

$(combined).click() 中的警报运行。

有任何想法吗?谢谢!:-)

编辑:这是我的最终代码:

   <script type="text/javascript">

        // hack to override the default postback of the form

        var oldPostBack;
        var abortPostback = false;

        window.onload = function () {
            oldPostBack = __doPostBack;
            __doPostBack = function () {
                if (!abortPostback) 
                {
                    oldPostBack();   
                }
            };
        };

        $(document).ready(function () {

            var id = $('input[value="1,3"]').attr('id');
            var combined = '#' + id;

            $(combined).click(function(e) {
                 abortPostback = true;
                mymagicmethod('parameter');
            });

        });
    </script>
4

1 回答 1

1

我认为 .net 运行时有 JS 浏览器检查单选按钮列表上的点击。preventDefault 和 stopPropagation 不会阻止其他侦听器执行。

但是,其他听众会尝试提交表单,因此如果您将代码放入 form.submit 中,那么您就有机会停止发回。

检查是否选择了值为 1,3 的单选按钮,如果是,则阻止默认并打开窗口。

于 2013-06-25T08:00:38.603 回答