在类中输入无线电添加名称

时间:2016-11-14 07:59:47

标签: javascript jquery html radio attr

<div typ="elementRadio" class="element elementRadio">
<input type="radio">
<input type="radio">
<input type="radio">
</div>

<div typ="elementRadio" class="element elementRadio">
<input type="radio">
<input type="radio">
<input type="radio">
</div>

.................
<div typ="elementRadio" class="element elementRadio">
<input type="radio">
<input type="radio">
<input type="radio">
</div>

他们需要添加一个随机名称Input。因此无线电输入在每个类中都有不同的名称。

我的代码

   $('.elementRadio input:radio').each(function () {
       $( "input:radio" ).attr( "name", "name1" );
       alert($( "input:radio" ).attr( "name"));
    });

所有名称

4 个答案:

答案 0 :(得分:3)

您应该遍历.elementRadio,然后将名称添加到input:radio

您可以使用index来计算唯一名称。

$('.elementRadio').each(function(i) {
  $(this).find('input:radio').attr("name", "name"+ (i+1));
  console.log($(this).find('input:radio').attr("name"));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div typ="elementRadio" class="element elementRadio">
  <input type="radio">
  <input type="radio">
  <input type="radio">
</div>

<div typ="elementRadio" class="element elementRadio">
  <input type="radio">
  <input type="radio">
  <input type="radio">
</div>

.................
<div typ="elementRadio" class="element elementRadio">
  <input type="radio">
  <input type="radio">
  <input type="radio">
</div>

答案 1 :(得分:1)

您可以迭代所有.element.elementRadio元素并使用jQuery.each()index

同时检查您是否必须纠正div中的typ="elementRadio"

代码:

$('.element.elementRadio').each(function (index) {
  $(this).find('input:radio').attr('name', 'name_' + index);

  // Console log: names
  console.log($(this).find('input:radio').attr('name'));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="element elementRadio">
  <input type="radio">
  <input type="radio">
  <input type="radio">
</div>

<div class="element elementRadio">
  <input type="radio">
  <input type="radio">
  <input type="radio">
</div>

<div class="element elementRadio">
  <input type="radio">
  <input type="radio">
  <input type="radio">
</div>

答案 2 :(得分:1)

检查一下。 我为每个无线电组进行了第一次迭代,然后将每个无线电输入设置在组的相同名称中。我还以数组格式命名,因此您可以获取数组中的数据。 希望这对你有用

$('.elementRadio').each(function (i) {
        var name = "name["+(i+1)+"]";
        $(this).find("input:radio").attr( "name", name );
            console.log(name );
        
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div typ="elementRadio" class="element elementRadio">
    <input type="radio">
    <input type="radio">
    <input type="radio">
</div>

<div typ="elementRadio" class="element elementRadio">
    <input type="radio">
    <input type="radio">
    <input type="radio">
</div>

.................
<div typ="elementRadio" class="element elementRadio">
    <input type="radio">
    <input type="radio">
    <input type="radio">
</div>

答案 3 :(得分:0)

尝试每个无线电输入的随机name

&#13;
&#13;
var count =1;
   $('.elementRadio input:radio').each(function (a) {
       if(a== 3)
         {   count=2; }
     else if(a == 6){count=3}
     $(this).attr( "name", "name"+count );
      console.log($(this).attr( "name"));
   
    });
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div typ="elementRadio" class="element elementRadio">
<input type="radio">
<input type="radio">
<input type="radio">
</div>

<div typ="elementRadio" class="element elementRadio">
<input type="radio">
<input type="radio">
<input type="radio">
</div>

.................
<div typ="elementRadio" class="element elementRadio">
<input type="radio">
<input type="radio">
<input type="radio">
</div>
&#13;
&#13;
&#13;