2013-06-25 55 views
1

我有一個帶AutoPostback的單選按鈕。點擊特定項目時,我想停止回發並顯示一個窗口。在ASP.NET窗體(radiobuttonlist)中停止回發 - 返回false並且preventDefault不起作用

我能夠停止傳播並顯示一個窗口,如果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> 

在$警報(組合) .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> 

回答

1

我認爲.NET運行時有JS liteners檢查的單選按鈕,列表上點擊。 preventDefault和stopPropagation不會阻止其他偵聽器執行。

然而,其他聽衆會嘗試提交表單,所以如果你把你的代碼放在form.submit中,那麼你有機會停止回發。

檢查是否選擇了值爲1,3的單選按鈕,如果是,則防止默認並打開窗口。

+1

謝謝!我們解決了這個問題,並且包含了代碼:-) –

相關問題