使用jQuery禁用单选按钮

前端之家收集整理的这篇文章主要介绍了使用jQuery禁用单选按钮前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图禁用这些单选按钮,当一个loadActive链接被点击,但由于某种原因,它只禁用顺序中的第一个,然后跳过其余。
  1. <form id="chatTickets" method="post" action="/admin/index.cfm/">
  2. <input id="ticketID1" type="radio" checked="checked" value="myvalue1" name="ticketID"/>
  3. <input id="ticketID2" type="radio" checked="checked" value="myvalue2" name="ticketID"/>
  4. </form>
  5. <a href="#" title="Load ActiveChat" id="loadActive">Load Active</a>

这里是我使用的jquery:

  1. jQuery("#loadActive").click(function() {
  2. //I have other code in here that runs before this function call
  3. writeData();
  4. });
  5. function writeData() {
  6. jQuery("input[name='ticketID']").each(function(i) {
  7. jQuery(this).attr('disabled','disabled');
  8. });
  9. }

解决方法

我重构你的代码,有点,这应该工作:
  1. jQuery("#loadActive").click(writeData);
  2.  
  3. function writeData() {
  4. jQuery("#chatTickets input:radio").attr('disabled',true);
  5. }

如果表单上有两个以上的单选按钮,则必须修改选择器,例如,您可以使用starts with attribute filter选择ID以ticketID开头的无线电:

  1. function writeData() {
  2. jQuery("#chatTickets input[id^=ticketID]:radio").attr('disabled',true);
  3. }

猜你在找的jQuery相关文章