检查第一个输入

Get the first input checked

本文关键字:输入 第一个 检查      更新时间:2023-09-26

我正在尝试设置中的第一个输入元素

<div class="containerplaatje klein"> 
    <div class=slides>
       <input...

"已检查",并从所有其他状态中删除"已检查的"状态。问题是:

每次我点击另一个容器时,它都必须将该容器中的第一个输入元素设置为"选中",并每隔一个释放一次。

见下面的小提琴:

https://jsfiddle.net/qa6not7w/

我以为这一次很接近,但我还是失败了:

$(document).ready(function info(){
$(".klein").click(function(){
 if ($(this).hasClass("containerplaatje")) {

    $(this).removeClass("containerplaatje");
    $(".containerplaatje2").addClass("containerplaatje");
    $(".containerplaatje2").removeClass("containerplaatje2");
    $(this).addClass("containerplaatje2");
    $(":input").attr ('checked', false);
  /*$('input:radio[name=radio-btn][id=img-1]').attr('checked',true);*/
   }
});

这将所有无线电的检查状态设置为false,然后将第一个设置为true:

$('input[type=radio]').prop('checked', false)[0].checked = true;

$('input[type=radio]').prop('checked', false)[0].checked = true;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="radio" />
<input type="radio" checked />
<input type="radio" />

请注意,在jQuery中使用了prop,在vanilla js中使用了属性,而不是jQuery中的attr和vanilla js中的setAttribute

这为我做到了:

      $('input[type=radio]').prop('checked', false);
      $(this).find('input[type=radio]').filter(':first').prop('checked', true);