How to select asp.net RadioButton with javascript when autopostback is on(启用自动回发时如何使用 javascript 选择 asp.net RadioButton)
问题描述
场景:
- 具有给定 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:启用自动回发时如何使用 javascript 选择 asp.net RadioButton
基础教程推荐
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- WPF 模态进度窗口 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
