Jquery函数仅在第1行上工作但在以下行

时间:2017-07-03 08:00:49

标签: javascript jquery

目前,代码仅适用于第一行,但无法处理以下行,任何想法?



if (typeof set == "undefined") {
  var set = $(".url").val();
}
$(".addSkip").on('click', function() {

  $(".url").val(set + $("#name").val());


});

<table>
  <tr>

    <td><input type="date" size="15" name="date[]" class="start_date" \>
      <input type="button" size="10" value="confirm date" class="addSkip"></td>
    <td>Nextdate<input type="text" value="100" id="name" size="15" name="nextdate[]" class="end_date" \> </td>

    <td>traditionalmessage<textarea rows="3" cols="20" readonly class="url" name="traditionalmessage[]">traditionalmessage</textarea> </td>
    
       <td>simplifiedmessage<textarea rows="3" cols="20" readonly class="url" name="simplified[]">simplifiedmessage</textarea> </td>
     <td>engmessage<textarea rows="3" cols="20" readonly class="url" name="engmessage[]">engmessage</textarea> </td>
    
    </tr>
    <script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

尝试使用classname而不是id。并找到元素us $(this).closest('tr').find(".url")

  1. closest()tr
  2. 的父级匹配
  3. find()匹配
  4. 中的儿童元素
  5. 你的html没有正确对齐
  6. <强>更新

    &#13;
    &#13;
    if (typeof set == "undefined") {
      var set = $(".url").val();
    }
    $(".addSkip").on('click', function() {
    
      $(this).closest('tr').find(".url").eq(0).val(set + $(this).closest('tr').find(".end_date").val());
    
    });
    &#13;
    <table>
      <tr>
    
        <td><input type="date" size="15" name="date[]" class="start_date" \>
          <input type="button" size="10" value="confirm date" class="addSkip"></td>
        <td>Nextdate<input type="text" value="100" id="name" size="15" name="nextdate[]" class="end_date" \> </td>
    
        <td>traditionalmessage<textarea rows="3" cols="20" readonly class="url" name="traditionalmessage[]">traditionalmessage</textarea> </td>
        
           <td>simplifiedmessage<textarea rows="3" cols="20" readonly class="url" name="simplified[]">simplifiedmessage</textarea> </td>
         <td>engmessage<textarea rows="3" cols="20" readonly class="url" name="engmessage[]">engmessage</textarea> </td>
        
        </tr>
        <script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
    &#13;
    &#13;
    &#13;