我和AutoPostback有一份单选按钮名单。单击特定项时,我希望停止回发并显示一个窗口。
我可以停止传播并显示一个autopostback=false窗口,所以我唯一需要的就是能够停止回发。
我曾尝试停止回发,但没有成功。因此:有关于如何停止回发的想法吗?我尝试过preventDefault()、e.stopImmediatePropagation()、e.stopPropagation()并返回false。
我的RadioButtonList:
<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>发布于 2013-06-25 16:00:39
我认为.net运行时让JS liteners检查单选按钮列表上的点击。preventDefault和stopPropagation不会阻止其他侦听器执行。
但是,其他侦听器将尝试提交表单,因此如果您将代码放在form.submit中,则有机会停止回发。
检查是否选中了值为1,3的单选按钮,如果选中,则阻止默认并打开窗口。
https://stackoverflow.com/questions/17291923
复制相似问题