我想在每行中添加count+。在第一个tr中,所有单选名称应为xRay1。在第二个tr中,所有单选名称应为xRay2。我试过的



$(document).ready(function() {
  $('.changeRadioName').click(function() {
    $('.table tr').each(function() {
      var count = 1;
      $(this).find('input:radio[name="xRay"]').attr('name', count);
      count++;
    });
  });
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<button class="changeRadioName">Change name of each row's radio</button>

<table class="table table-bordered table-striped table-hover">
  <tr>
    <td>
      <label><input type="radio" class="trBtnYes" name="xRay" value="true" /> Yes</label>
      <label><input type="radio" class="trBtnNo" name="xRay" value="false" /> No</label>
    </td>
  </tr>
  <tr>
    <td>
      <label><input type="radio" class="trBtnYes" name="xRay" value="true" /> Yes</label>
      <label><input type="radio" class="trBtnNo" name="xRay" value="false" /> No</label>
    </td>
  </tr>
  <tr>
    <td>
      <label><input type="radio" class="trBtnYes" name="xRay" value="true" /> Yes</label>
      <label><input type="radio" class="trBtnNo" name="xRay" value="false" /> No</label>
    </td>
  </tr>
  <tr>
    <td>
      <label><input type="radio" class="trBtnYes" name="xRay" value="true" /> Yes</label>
      <label><input type="radio" class="trBtnNo" name="xRay" value="false" /> No</label>
    </td>
  </tr>
</table>

最佳答案

我试图得到你的结果,
在每个函数中使用计数器变量



 $(document).ready(function(){

    $('.changeRadioName').click(function(){
            var count = 1;
    $('.table tr').each(function () {
                $(this).find('input:radio[name="xRay"]').attr('name', 'xRay'+count);

                count++;
            });
         });
 });

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

 <button class="changeRadioName">Change name of each row's radio</button>

    <table class="table table-bordered table-striped table-hover">
    <tr>
        <td>
              <label><input type="radio" class="trBtnYes" name="xRay" value="true" /> Yes</label>
              <label><input type="radio" class="trBtnNo" name="xRay" value="false" /> No</label>
        </td>
        </tr>
    <tr>
        <td>
              <label><input type="radio" class="trBtnYes" name="xRay" value="true" /> Yes</label>
              <label><input type="radio" class="trBtnNo" name="xRay" value="false" /> No</label>
        </td>
        </tr>
    <tr>
        <td>
              <label><input type="radio" class="trBtnYes" name="xRay" value="true" /> Yes</label>
              <label><input type="radio" class="trBtnNo" name="xRay" value="false" /> No</label>
        </td>
        </tr>
    <tr>
        <td>
              <label><input type="radio" class="trBtnYes" name="xRay" value="true" /> Yes</label>
              <label><input type="radio" class="trBtnNo" name="xRay" value="false" /> No</label>
        </td>
        </tr>
    </table>

关于javascript - 如何使用jQuery在每一行的单选按钮中添加计数?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53610168/

10-09 05:41