下面的代码将一个值添加到文本框中:
$('#mainTxtWeight').val(textWeight);
但我想知道的是如何根据使用 jquery 的值来选择单选按钮?
下面是单选按钮:
<table id="replies">
<tr>
<th colspan="2">Replies</th>
</tr>
<tr>
<td>Number of Replies:</td>
<td align="left">
<div class="replytd">
<label>
<input type="radio" name="reply" id="singlebtn " value="single" class="replyBtn"
/>Single</label>
</div>
<div class="replytd">
<label>
<input type="radio" name="reply" id="multiplebtn" value="multiple" class="replyBtn"
/>Multiple</label>
</div>
</td>
</tr>
</table>
我在下面创建了一个 if 语句,如果回复类型等于“单个”,我只需要帮助选择“单个”单选按钮,或者如果回复类型等于“多个”,则选择“多个”单选按钮。
下面是if语句:
if(reply == "Single") {
//select "Single" radio button
} else if(reply == "Multiple") {
//select "Multiple" radio button
}
我希望这行代码也能发生完全相同的事情,这又是“单个”和“多个”单选按钮:
var $btnReply = "<input type='radio' name='reply[" + count + "]' value='single' class='replyBtnRow' /> Single<br /><input type='radio' name='reply[" + count + "]' value='multiple' class='replyBtnRow' /> Multiple";
$replies.children().append($btnReply);
if($("input[name=reply]:checked").length) {
$replies.find('input[name="reply[' + count + ']"]').eq($("input[name=reply]:checked").val() == "single" ? 0 : 1).prop("checked", true);
}
更新:
function addwindow(reply) {
if($(plusbutton_clicked).attr('id')=='mainPlusbutton') {
$('input[value="'+reply.toLowerCase()+'"]').attr('checked', true);
} else {
$(plusbutton_clicked).closest('tr').find('input[value="'+reply.toLowerCase()+'"]').attr('checked', true);
}
$.modal.close();
return false;
}