我有以下代码。尝试根据传入的数组值预填充单选按钮。将会是1-3
<input name="rating1" type="radio" value="1" />
<input name="rating1" type="radio" value="2" />
<input name="rating1" type="radio" value="3" />
$(document).ready(function() {
//Setting up the radio buttons
var $radios = $('input:radio[name=rating1]');
//ratingAnswer is the value I need to select in the radio group. Value=2
var ratingAnswer=answersArray[oneMap].RATING;
if($radios.is(':checked') === false) {
$radios.filter('[value=ratingAnswer]').attr('checked', true);
}这不会返回任何结果,因为它查找字符串ratingAnswer作为值,而不是ratingAnswer的变量值。我试过value=‘+ ratingAnswer +’,但它一点也不喜欢。如何将变量注入到值区域。提前谢谢。
发布于 2012-09-15 21:13:49
您应该更改此行:
$radios.filter('[value=ratingAnswer]').attr('checked', true);对于这一条:
$radios.filter('[value='+ratingAnswer+']').attr('checked', true);https://stackoverflow.com/questions/12437643
复制相似问题