问题描述
场景:
- 具有给定 GroupName 和
AutoPostBack="true" 的多个单选按钮.
- 出于样式目的,单选按钮使用 js 隐藏,对其容器(td)的点击通过 js 处理
- 当点击 td 时,脚本点击"输入元素并触发 asp.net 的自动回发
它在服务器上进行回发并点击 PageLoad,但未触发代码隐藏中的事件.
It does postback and hits PageLoad on the server, but the event in the codebehind is not triggered.
推荐答案
我发布这个以防万一有人不幸遇到这个问题.
I posted this in case anyone is unfortunate enough to hit this problem.
在对此答案的评论中非常准确地提到了问题https://stackoverflow.com/a/8244315/66372.
Problem is very accurately mentioned in a comment on this answer https://stackoverflow.com/a/8244315/66372.
但是 .click() 有一件事:如果您像这样使用 javascript 更改收音机的选定值,则更改"事件不会在 IE 中触发(我尝试过 IE8)– Michiel Reyers
There is however one thing with the .click(): If you change the selected value of a radio with javascript like this, the 'change' event does not fire in IE (I tried IE8) – Michiel Reyers
这似乎与 asp.net 的回发事件处理相混淆.所以为了让它捡起来,我们首先明确地选择它:
That seems to mess with asp.net's postback event handling. So for it to pick it up, we explicitly select it first:
$(this).find("input").prop("checked", true);
$(this).find("input").click();
这篇关于启用自动回发时如何使用 javascript 选择 asp.net RadioButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!