检查单选按钮是否存在 jQuery
Check if radio button exists jQuery
我正在根据特定值将单选按钮的选中状态设置为 true;
var radioValue = data.Activity.Answer == "Please add your answer" ? "valueA" : data.Activity.Answer;
$("input[name=optionRadio" + currentIndex + "][value=" + radioValue + "]").attr('checked', true);
我事先检查幻灯片上是否确实存在该单选按钮的最佳方法是什么?
使用length
if($("input[name=optionRadio" + currentIndex + "][value=" + radioValue + "]").length)
但是请注意,如果单选按钮不存在,调用 attr
将没有任何效果,也不会导致任何错误。因此,如果您的唯一目标是设置属性,则不需要 if
语句。
你可以查看jQuery对象的length
找出来:
if($("input[name=optionRadio" + currentIndex + "]").length > 0 ){
// Radio button actually exists...
}
我正在根据特定值将单选按钮的选中状态设置为 true;
var radioValue = data.Activity.Answer == "Please add your answer" ? "valueA" : data.Activity.Answer;
$("input[name=optionRadio" + currentIndex + "][value=" + radioValue + "]").attr('checked', true);
我事先检查幻灯片上是否确实存在该单选按钮的最佳方法是什么?
使用length
if($("input[name=optionRadio" + currentIndex + "][value=" + radioValue + "]").length)
但是请注意,如果单选按钮不存在,调用 attr
将没有任何效果,也不会导致任何错误。因此,如果您的唯一目标是设置属性,则不需要 if
语句。
你可以查看jQuery对象的length
找出来:
if($("input[name=optionRadio" + currentIndex + "]").length > 0 ){
// Radio button actually exists...
}