我正在使用一个Webforms页面,我在其中添加了一个简单的YES/NO单选按钮。 我创建了一个查询单选按钮的JavaScript函数,它在Chrome上工作正常,但在IE8(企业标准浏览器)上失败。 我很困惑,试图想出一种方法来查看使用 IE8 运行时单击了哪个按钮。 这是 HTML 页面中的按钮:
<td width="35%">
<label class="label_bold floatLeft" style="margin-top: .25em">Clinicals sent:</label>
</td>
<td width="15%">
<asp:RadioButtonList ID="rbtClinicalsSent"
AutoPostBack="true"
RepeatLayout="Flow"
RepeatDirection="Horizontal"
OnClick="rbtClinicalsSent_clicked()"
OnTextChanged="rbtClinicalsSent_TextChanged"
runat="server">
<asp:ListItem Value="1">Yes</asp:ListItem>
<asp:ListItem Value="0">No</asp:ListItem>
</asp:RadioButtonList>
</td>
我的JavaScript函数(从StackOverflow上的其他地方复制)在Chrome上工作正常(没有尝试过Firefox)。
function rbtClinicalsSent_clicked() {
/* This is based on the assumption that the first element
in the radio button is Yes */
var yesButtonClicked = false;
var radioButton;
radioButton = document.getElementsByName("rbtClinicalsSent");
for (i= 0; i< radioButton.length; i++){
if (radioButton[i].checked & rbtIdx===0)
yesButtonClicked = true;
};
return yesButtonClicked;
};
当我使用 IE8 运行调试器并查看可用于"rbtClinicalsSent"单选按钮的方法时,似乎没有什么可用于检查"单击"状态。 我认为,一个理想的解决方案是转储IE8,这在这种企业环境中是不可能的。
蒂亚斯科特
我找到了答案。 在IE8中,radioButton[0]包含一些元数据,但不包含选中的属性。 可能有点有用的属性是innerText,对于两个单选按钮说"是"和"否",innerText是"YesNo"。 修复是检查"typeof(RadioButton[i])==="undefined"。 以下是适用于IE8,Chrome和Firefox的相同功能。
function rbtClinicalsSentYes_clicked() {
/* This is based on the assumption that the first element
in the radio button is Yes */
var yesButtonClicked = false;
var radioButton;
radioButton = document.getElementsByName("rbtClinicalsSent");
for (i= 0; i< radioButton.length; i++){
if (typeof(radioButton[i].checked) === "undefined") {
continue;
}
if (radioButton[i].checked & rbtIdx===0)
yesButtonClicked = true;
};
return yesButtonClicked;
};