为什么无论询问的单选按钮是否被检查,这个检查都是真的?
Why is this check true whether or not the interrogated radio buttons are checked?
我有这个 jQuery 来检查当用户选择下拉列表时是否选中了上面两个单选按钮之一
$(document).on("change", '[id$=ddlPayToIndividual]', function () {
. . . // unrelated code elided for brevity
var $uscitizenyes = $('[id$=rbUSCitizenOrPermResY]');
var $uscitizenno = $('[id$=rbUSCitizenOrPermResN]');
if (!$uscitizenyes.checked && !$uscitizenno.checked) {
alert('You must select above whether payee is a US Citizen or Permanent Resident or not');
}
});
我想只有在两个单选按钮都被选中时我才会看到警报,但是无论我在[=17=中更改选择时我都会看到它] 选择器。为什么?
.checked
是 DOM 属性,但 $uscitizenyes
和 $uscitizenno
是 jQuery 对象,而不是 DOM 元素.您需要使用 jQuery 方法。使用:
if (!$uscitizenyes.is(":checked") && !$uscitizenno.is(":checked")) {
因为 jQuery 对象没有 .checked
属性(一般来说,jQuery 不使用属性,它几乎用方法完成所有事情),您的代码始终认为两个框都未选中。
我有这个 jQuery 来检查当用户选择下拉列表时是否选中了上面两个单选按钮之一
$(document).on("change", '[id$=ddlPayToIndividual]', function () {
. . . // unrelated code elided for brevity
var $uscitizenyes = $('[id$=rbUSCitizenOrPermResY]');
var $uscitizenno = $('[id$=rbUSCitizenOrPermResN]');
if (!$uscitizenyes.checked && !$uscitizenno.checked) {
alert('You must select above whether payee is a US Citizen or Permanent Resident or not');
}
});
我想只有在两个单选按钮都被选中时我才会看到警报,但是无论我在[=17=中更改选择时我都会看到它] 选择器。为什么?
.checked
是 DOM 属性,但 $uscitizenyes
和 $uscitizenno
是 jQuery 对象,而不是 DOM 元素.您需要使用 jQuery 方法。使用:
if (!$uscitizenyes.is(":checked") && !$uscitizenno.is(":checked")) {
因为 jQuery 对象没有 .checked
属性(一般来说,jQuery 不使用属性,它几乎用方法完成所有事情),您的代码始终认为两个框都未选中。