0

下面的代码将一个值添加到文本框中:

$('#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;
} 
4

3 回答 3

1
$('input[name="reply"]').filter('[value="' + reply.toLowerCase() + '"]')
                        .attr('checked', true);
于 2012-07-06T23:13:23.183 回答
1

如果您尝试获取具有回复内容值的单选按钮,如果我正确理解您的问题,您可以试试这个

$('input[value="'+reply.toLowerCase()+'"]')​.attr('checked', true)

假设回复将始终是“单个”或“多个”

于 2012-07-06T23:17:28.627 回答
0
if(reply == "Single")
    $("#singlebtn").attr('checked',true);

else if(reply == "Multiple")
    $("#multiplebtn").attr('checked',true);
于 2012-07-06T23:12:12.397 回答