0

我有这组标签和单选按钮,如下所示:

Entertainment: <input type="radio" name="entertainment" id="entertainment" value="0" checked/>
               <input type="radio" name="entertainment" id="entertainment" value="1" /> 
               <input type="radio" name="entertainment" id="entertainment" value="2" /><br />


Radio: <input type="radio" name="radio" id="radio" value="0" checked/>
       <input type="radio" name="radio" id="radio" value="1" /> 
       <input type="radio" name="radio" id="radio" value="2" /><br />

Dancing: <input type="radio" name="dancing" id="dancing" value="0" checked/>
         <input type="radio" name="dancing" id="dancing" value="1" /> 
         <input type="radio" name="dancing" id="dancing" value="2" /><br />

Music: <input type="radio" name="music" id="music" value="0" checked/>
       <input type="radio" name="music" id="music" value="1" /> 
       <input type="radio" name="music" id="music" value="2" /><br />

Television: <input type="radio" name="tv" id="tv" value="0" checked/>
            <input type="radio" name="tv" id="tv" value="1" /> 
            <input type="radio" name="tv" id="tv" value="2" /><br />

Film: <input type="radio" name="film" id="film" value="0" checked/>
      <input type="radio" name="film" id="film" value="1" /> 
      <input type="radio" name="film" id="film" value="2" /><br />

Theatre: <input type="radio" name="theatre" id="theatre" value="0" checked/>
         <input type="radio" name="theatre" id="theatre" value="1" /> 
         <input type="radio" name="theatre" id="theatre" value="2" />

这个想法是顶部的三个“娱乐”单选按钮控制下面的所有单选按钮。如果您愿意,可以使用“全局切换”来节省人们的时间。有谁知道完成此操作所需的 JavaScript/jQuery 吗?我在网上找不到正确的例子。

4

1 回答 1

1
$(':radio[name=entertainment]').change(function() {
  $(this).nextAll(':radio[value="'+ this.value +'"]').prop('checked', true);
  $(this).nextAll(':radio[value!="'+ this.value +'"]').prop('checked', false);
});

工作样本

笔记

ids 应该是唯一的。

于 2012-06-25T15:47:04.403 回答