-2

我有这两种形式:

  <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>

如果选中任何单选框,我希望取消选中所有复选框,并且如果选中任何复选框,我希望取消选中选定的单选框。

实现这一目标的最简单方法是什么?

4

4 回答 4

2

像这样?:

$('[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);
});
于 2013-09-25T04:05:44.183 回答
1

尝试

var $checks = $('input[name="vehicle"]').change(function(){
    if(this.checked){
        $rsdios.prop('checked', false)
    }
})
var $rsdios = $('input[name="sex"]').change(function(){
    if(this.checked){
        $checks.prop('checked', false)
    }
})

演示:小提琴

于 2013-09-25T04:06:34.117 回答
1
$('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');
});
于 2013-09-25T04:04:09.737 回答
0

我已经修改了您的 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>

并在脚本中添加以下方法

功能取消检查(obj,名称){

        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);
            });
        }
    }
于 2013-09-25T05:15:16.823 回答