如何使用jquery和ajax检查和取消选中单选按钮?

时间:2014-02-18 09:36:30

标签: jquery radio-button uncheck

我正在使用ajax选择动态生成的单选按钮,当我尝试取消选中单选按钮时,我不知道它是如何工作的。所以有人可以帮助我吗?

以下是动态生成的HTML代码:

enter code here
<table cellepadding='15' cellspacing='15' border='1' class='gridtable' align='center'>
<tr>
<td><strong>Service Name</strong></td>
<td><strong>Daily</strong>
<td><strong>Weekly</strong></td>
<td><strong>BiMonthly</strong></td>
<td><strong>Monthly</strong></td>
</tr>

<tr>
<td>Inspection & Reporting</td>
<td>NA</td>
<td align="center">
<input type="radio" id="215" name="1" value="1/215" onclick="DisplayPrice(this.id);" class="radio"/> <br> Rs. <strong>50</strong></td>
<td><input type="radio" id="200" name="1" value="2/200" onclick="DisplayPrice(this.id);" class="radio"/>  <br> Rs. <strong>100</strong></td>
<td align="center"><input type="radio" id="200" name="1" value="3/200" onclick="DisplayPrice(this.id);" class="radio"/> <br> Rs. <strong>200</strong></td>
</tr>

<tr>
<td>House keeping/Cleaning</td>
<td>NA</td>
<td align="center"><input type="radio" id="322.5" name="2" value="7/322.5" onclick="DisplayPrice(this.id);" class="radio"/> <br> Rs. <strong>75</strong></td>
<td><input type="radio" id="300" name="2" value="8/300" onclick="DisplayPrice(this.id);" class="radio"/>  <br> Rs. <strong>150</strong></td>
<td align="center"><input type="radio" id="300" name="2" value="9/300" onclick="DisplayPrice(this.id);" class="radio"/> <br> Rs. <strong>300</strong></td>
</tr>

<tr>
<td>Utility Bill Payment</td>
<td>NA</td>
<td align="center"><input type="radio" id="258" name="3" value="14/258" onclick="DisplayPrice(this.id);" class="radio"/> <br> Rs. <strong>60</strong></td>
<td><input type="radio" id="240" name="3" value="15/240" onclick="DisplayPrice(this.id);" class="radio"/>  <br> Rs. <strong>120</strong></td>
<td align="center"><input type="radio" id="250" name="3" value="13/250" onclick="DisplayPrice(this.id);" class="radio"/> <br> Rs. <strong>250</strong></td>
</tr>

<tr>
<td>Plumbing Inspection</td>
<td>NA</td>
<td align="center"><input type="radio" id="129" name="4" value="19/129" onclick="DisplayPrice(this.id);" class="radio"/> <br> Rs. <strong>30</strong></td>
<td><input type="radio" id="120" name="4" value="20/120" onclick="DisplayPrice(this.id);" class="radio"/>  <br> Rs. <strong>60</strong></td>
<td align="center"><input type="radio" id="240" name="4" value="21/240" onclick="DisplayPrice(this.id);" class="radio"/> <br> Rs. <strong>240</strong></td>
</tr>

</table>

5 个答案:

答案 0 :(得分:6)

使用这些语句检查/取消选中单选按钮

    $('#radioID').attr('checked', true) - Check the radio button which has ID "radioID"
    $('#radioID').attr('checked', false) - This is to uncheck the radio button

如果您想知道单选按钮是否已被选中,请使用此语句

    var checked = $('#radioID').attr('checked');

答案 1 :(得分:4)

答案 2 :(得分:0)

试试这个,它对我有用。

document.getElementById('Usertype1').checked = true;

答案 3 :(得分:0)

使用此代码,您可以轻松地选中并取消选中您的单选按钮:

    $('.btnRadio').bind('change', function() {
        $(this).attr('checked','checked');
    });
    $('.btnRadio').bind('click', function() {
        $(this).removeAttr('checked');
    });

但这只适用于Firefox。我使用双击来解决问题:

    $('.btnRadio').on('dblclick', function() {
        $(this).removeAttr('checked');
    });

答案 4 :(得分:0)

没有其他解决方案对我有用-他们确实更改了单选按钮的值,但是没有更改视觉显示,即确实删除了“选中”属性,但用户仍然看到了单选按钮

这是DID为我工作的方式:

$("#radioID").prop("checked", false);
相关问题