jQuery 1.7.2 - 在IE 8中中断了selector.val()

时间:2012-05-23 18:15:24

标签: javascript jquery internet-explorer-8

我的HTML

中有以下块
<div class="selection">
  <p><input name="custrecord_npsdtl_decision" id="custrecord_npsdtl_decision" type="radio" value="I am solely responsible for the decision">I am solely responsible for the decision </p>   
  <p><input name="custrecord_npsdtl_decision" id="custrecord_npsdtl_decision" type="radio" value="I am partially responsible for or influence the decision">I am partially responsible for or influence the decision </p>
  <p><input name="custrecord_npsdtl_decision" id="custrecord_npsdtl_decision" type="radio" value="I am not involved in the decision">I am not involved in the decision </p>
  <p><input name="custrecord_npsdtl_decision" id="custrecord_npsdtl_decision" type="radio" value="Don't know">Don't know </p>
</div>

根据所选选项检查表单进度的功能,使用以下代码段:

var decisionmaker = $('#custrecord_npsdtl_decision:checked').val();
if (decisionmaker == elements['customlist_nps_decision'][2] || decisionmaker == elements['customlist_nps_decision'][3]) {
  redirect();
}

这在Chrome,Firefox中可以正常使用,但在Internet Explorer(8)中无效。做了一些检查我发现

$('#custrecord_npsdtl_decision:checked')

按预期返回一个对象,但在对象上调用val()会返回undefined。

我绝对不知所措。如何从IE中的单选列表中获取所选选项?

2 个答案:

答案 0 :(得分:5)

ID必须是唯一的。我很惊讶这在Chrome和Firefox中都有用。

删除id属性并按名称选择。

$('input[name="custrecord_npsdtl_decision"]:checked').val();

答案 1 :(得分:2)

你不应该为每个单选按钮使用相同的ID,你可以使用名称选择器来获取组选择

$('input[name=custrecord_npsdtl_decision]:checked').val();