Jquery 如果选中了“单选框”,则取消选中复选框,反之亦然?

Jquery 如果选中了“单选框”,则取消选中复选框,反之亦然?,jquery,html,Jquery,Html,我有以下两种形式: <div class="pricing-details pricing-details-downloads"> <h4>Single purchase (60 lessons)</h4> <h4>Bulk Purchase: Lesson</h4> <div class="pricing-details-separator"></div> <form

我有以下两种形式:

  <div class="pricing-details pricing-details-downloads">
    <h4>Single purchase (60 lessons)</h4>
    <h4>Bulk Purchase: Lesson</h4>
    <div class="pricing-details-separator"></div>
    <form action="">
      <input type="checkbox" name="vehicle" value="Bike">Lesson Audio / <span class="pricing-box-price">$19.95 USD</span><br>
      <input type="checkbox" name="vehicle" value="Bike">Lesson PDFs / <span class="pricing-box-price">$19.95 USD</span><br>
      <input type="checkbox" name="vehicle" value="Bike">Review Audio / <span class="pricing-box-price">$19.95 USD</span><br>
    </form>
    <form class="pricing-last-form" action="">
      <input type="radio" name="sex" value="male"><strong>Single Level Download $49.95:</strong> Choose a single level (1 to 7) and receive access to all lessons, PDFs and review audio for that level, plus 30 days of online access ($80 value). Email us your level choice after purchase.<br>
      <input type="radio" name="sex" value="male"><strong>3 Level Download $99.95:</strong> Choose any 3 levels (1 to 7) and receive access to all lessons, PDFs and review audio for those levels, plus 60 days of online access ($190 value). Email us your level choices after purchase.<br>
      <input type="radio" name="sex" value="male"><strong>All Access Package $199.9:</strong> Receive access to all lessons, PDFs and review audio for all 7 levels, plus 90 days of online access ($300 value).<br>
    </form>
  </div>

单次购买(60课)
大宗采购:教训
课程音频/19.95美元
第一课PDF/$19.95美元
回顾音频/19.95美元
单级下载$49.95:选择一个单级(1到7级)并获得该级别的所有课程、PDF和音频回顾,加上30天的在线访问(价值80美元)。购买后通过电子邮件向我们发送级别选择。
3级下载$99.95:选择任意3级(1至7级),即可访问这些级别的所有课程、PDF和音频,再加上60天的在线访问(价值190美元)。购买后通过电子邮件向我们发送级别选择。
所有访问套餐$199.9:可访问所有课程、PDF和7个级别的音频,加上90天的在线访问(价值300美元)。
如果选中任何单选框,我希望取消选中所有复选框;如果选中任何复选框,我希望取消选中所选单选框

最简单的方法是什么

像这样

$('input[type=radio]').change(function(){
    if($('input[type=radio]:checked').length > 0)
        $('input[type=checkbox]').removeAttr('checked');
});

$('input[type=checkbox]').change(function(){
    if($('input[type=checkbox]:checked').length > 0)
        $('input[type=radio]').removeAttr('checked');
});
$('[name="sex"]').on('change', function(){ // if your jquery version doesn't support on then use .change
      $('[name="vehicle"]').prop('checked', false);
});
$('[name="vehicle"]').on('change', function(){
      $('[name="sex"]').prop('checked', false);
});

或者把它们结合起来

$('[name="sex"], [name="vehicle"]').on('change', function(){ //or target ':checkbox, :radio'
    var other = this.name =="vehicle" ? "sex" : "vehicle";
     $('[name='+other + ']').prop('checked', false);
});
试一试


演示:

我已经像下面那样修改了您的HTML,为复选框和单选按钮添加取消选中事件

<form action="">
            <input type="checkbox" onclick="Uncheck(this, 'sex')" name="vehicle" value="Bike"/>Lesson Audio / <span class="pricing-box-price">$19.95 USD</span>
            <br/>
            <input type="checkbox" onclick="Uncheck(this, 'sex')" name="vehicle" value="Bike"/>Lesson PDFs / <span class="pricing-box-price">$19.95 USD</span>
            <br/>
            <input type="checkbox" onclick="Uncheck(this, 'sex')" name="vehicle" value="Bike"/>Review Audio / <span class="pricing-box-price">$19.95 USD</span>
            <br/>
        </form>
        <form class="pricing-last-form" action="">
            <input type="radio" onclick="Uncheck(this, 'vehicle')" name="sex" value="male"/><strong>Single Level Download $49.95:</strong> Choose a single level (1 to 7) and receive access to all lessons, PDFs and review audio for that level, plus 30 days of online access ($80 value). Email us your level choice after purchase.
            <br/>
            <input type="radio" onclick="Uncheck(this, 'vehicle')" name="sex" value="male"/><strong>3 Level Download $99.95:</strong> Choose any 3 levels (1 to 7) and receive access to all lessons, PDFs and review audio for those levels, plus 60 days of online access ($190 value). Email us your level choices after purchase.
            <br/>
            <input type="radio" onclick="Uncheck(this, 'vehicle')" name="sex" value="male"/><strong>All Access Package $199.9:</strong> Receive access to all lessons, PDFs and review audio for all 7 levels, plus 90 days of online access ($300 value).
            <br/>
        </form>

谢谢,但是有没有其他不以名字为目标的方法?这些可能会改变。Target:radio和:checkboxI在更改时没有任何方法?@alexchenco:
change()
单击
不是一个好事件,因为您也可以使用键盘导航并点击
空格
。。。
<form action="">
            <input type="checkbox" onclick="Uncheck(this, 'sex')" name="vehicle" value="Bike"/>Lesson Audio / <span class="pricing-box-price">$19.95 USD</span>
            <br/>
            <input type="checkbox" onclick="Uncheck(this, 'sex')" name="vehicle" value="Bike"/>Lesson PDFs / <span class="pricing-box-price">$19.95 USD</span>
            <br/>
            <input type="checkbox" onclick="Uncheck(this, 'sex')" name="vehicle" value="Bike"/>Review Audio / <span class="pricing-box-price">$19.95 USD</span>
            <br/>
        </form>
        <form class="pricing-last-form" action="">
            <input type="radio" onclick="Uncheck(this, 'vehicle')" name="sex" value="male"/><strong>Single Level Download $49.95:</strong> Choose a single level (1 to 7) and receive access to all lessons, PDFs and review audio for that level, plus 30 days of online access ($80 value). Email us your level choice after purchase.
            <br/>
            <input type="radio" onclick="Uncheck(this, 'vehicle')" name="sex" value="male"/><strong>3 Level Download $99.95:</strong> Choose any 3 levels (1 to 7) and receive access to all lessons, PDFs and review audio for those levels, plus 60 days of online access ($190 value). Email us your level choices after purchase.
            <br/>
            <input type="radio" onclick="Uncheck(this, 'vehicle')" name="sex" value="male"/><strong>All Access Package $199.9:</strong> Receive access to all lessons, PDFs and review audio for all 7 levels, plus 90 days of online access ($300 value).
            <br/>
        </form>
        if (obj.type == 'checkbox') {
            $('input:radio[name=' + name + ']').each(function () {

                $(this).attr('checked', false);
            });

        } else if (obj.type == 'radio') {
            $('input:checkbox[name=' + name + ']').each(function () {

                $(this).attr('checked', false);
            });
        }
    }