简体   繁体   English

如果单击div中的单选按钮,则单击其他div中的相应单选按钮

[英]If radio button in div is clicked then corresponding radio button in other div is clicked

Can someone help me with a better way to write this? 有人可以用更好的方式来帮我写这个吗? So we are going to be adding to the list of levels and thumbnails and I would like this to work dynamically. 所以我们将添加到级别和缩略图列表中,我希望它能够动态地工作。 So say if you selected level 15 then level 15 in the all-thumbnails div would be selected. 所以说如果你选择了15级,那么将选择全缩略图div中的15级。 Currently I have a script that is limited and would have to be repeated for the number of levels/thumbnails we create. 目前我有一个有限的脚本,必须重复我们创建的级别/缩略图的数量。

 $( "#all-levels input:eq(0)" ).click(function() { $( "#all-thumbnails input:eq(0)" ).click(); }); $( "#all-levels input:eq(1)" ).click(function() { $( "#all-thumbnails input:eq(1)" ).click(); }); 
 input {float: left;} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> <div id="all-levels"> <div class="level"> <div class="level-container"> <input name="level-expand" id="level-expand587" value="3587" type="radio"> </div> <label for="level-expand3587" onclick=""> <div class="level-amount-container"> $18.00 </div> <div class="level-label-container"> Chai </div> </label> </div> <div class="level"> <div class="level-container"> <input name="level-expand" id="level-expand3589" value="3589" type="radio"> </div> <label for="level-expand3589" onclick=""> <div class="level-amount-container"> $25.00 </div> <div class="level-label-container"> Classic Frame </div> </label> </div> </div> <p>&nbsp;</p> <div id="all-thumbnails"> <div class="thumbnail-container"> <label class="thumbnail-label" for="layout_id_1201"> <img src="http://www.imagemagick.org/Usage/thumbnails/thumbnail.gif" alt="Chai" border="0"> </label> <input name="layout_id" id="layout_id_1201" value="1201" type="radio"> </div> <div class="thumbnail-container"> <label class="thumbnail-label" for="layout_id_2456"> <img src="https://mediaarchive.cern.ch/MediaArchive/Video/Public/Movies/CERN/2013/CERN-MOVIE-2013-051/CERN-MOVIE-2013-051-010/CERN-MOVIE-2013-051-010-thumbnail-135x101-at-5-percent.jpg" alt="Chai" border="0"> </label> <input name="layout_id" id="layout_id_2456" value="2456" type="radio"> </div> </div> 

The current script clicks the corresponding radio button in the all-thumbnails div. 当前脚本单击全缩略图div中的相应单选按钮。 Is there a way to do this without writing eq:(0), eq:(1), eq:(2) and so on so that no matter how many levels and thumbnails are added it will work dynamically? 有没有办法做到这一点,而无需编写eq:(0),eq:(1),eq:(2)等等,这样无论添加多少级别和缩略图,它都会动态工作?

side note : the thumbnails section will be hidden from front end users, in case that is important. 旁注 :如果重要的话,缩略图部分将隐藏在前端用户之外。

Well you could find position index of element which was clicked, and then just query input element from another div with the index you just got. 那么你可以找到被点击的元素的位置索引,然后只用你刚刚获得的索引从另一个div查询输入元素。 So if you click on third element in #all-levels div , then third element in #all-thumbnails will get click as well. 因此,如果您单击#all-levels div第三个元素,那么#all-thumbnails第三个元素也会被点击。

$( "#all-levels input" ).on('click', function(){
    var index = $( "#all-levels input" ).index(this);
    $( "#all-thumbnails input").eq(index).click();
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM