2011-03-31 63 views
0

我正在使用JQM(jQueryMobile),並且需要選中組中的所有複選框,直到選中一個複選框,然後只需要選中一個複選框,除非它是全部檢查所有。需要要求所有複選框goup,直到選中一個

問題是我不能使用複選框組的Id或Name屬性,所以我試圖使用class屬性。

這裏是標記的例子(但功能不工作):http://jsfiddle.net/HfLq3/7/

HTML:

<div data-role="page"> 
    <form id="chips_form" name="chips_form" action="#" method="post"> 
     <div data-role="fieldcontain"> 
     <fieldset data-role="controlgroup"> 
      <legend>Choose as many snacks as you'd like:</legend> 
      <input type="checkbox" name="checkbox-1a" id="checkbox-1a" class="custom chips" /> 
      <label for="checkbox-1a">Cheetos</label> 

      <input type="checkbox" name="checkbox-2a" id="checkbox-2a" class="custom chips" /> 
      <label for="checkbox-2a">Doritos</label> 

      <input type="checkbox" name="checkbox-3a" id="checkbox-3a" class="custom chips" /> 
      <label for="checkbox-3a">Fritos</label> 

      <input type="checkbox" name="checkbox-4a" id="checkbox-4a" class="custom chips" /> 
      <label for="checkbox-4a">Sun Chips</label> 

      <input type="checkbox" name="checkbox-5a" id="checkbox-5a" class="custom chips" /> 
      <label for="checkbox-5a">Select All</label> 
     </fieldset> 
     </div> 
     <div data-role="fieldcontain"> 
      <button type="submit" name="submit" value="chips-submit">Submit</button> 
     </div> 
    </form> 
</div> 

JS:

$(".chips").change(function() { 
    alert('selected Value(s): '+$(this).val()); 
}); 

$('.chips').each(function() { 
    $(this).attr('required','required'); 
}); 

回答

1
$(".chips").change(function() { 
    $('.chips').attr('required', $(this).attr("checked") ? '': 'required'); 
}); 

$('.chips').attr('required','required'); 
+0

它的工作原理,除非我檢查,然後取消檢查,表格仍然提交沒有檢查 – 2011-03-31 19:22:28

+0

嗯,工作正常。檢查http://jsfiddle.net/HfLq3/9/ – 2011-03-31 19:28:01

相關問題