2017-02-13 74 views
0

当单击按钮时,我想将输入字段中的相同文本附加到其他多个字段。jQuery将文本附加到多个输入字段

这里是形式:

<div class="affiliate-id-form"> 
    <input type="text" name="affiliateID" id="affiliateID" placeholder="Enter your affiliate ID eg. 124531" /> 
    <input type="button" name="generate-links" id="btnGenerateLinks" value="Generate Links"> 
</div> 

这里就是我想的要追加的文本输入字段的代码。

<div class="affiliate-links"> 
    <table class="affiliateLinksTable"> 
     <tbody> 
      <tr> 
       <td><label for=""> FIrst Product Name</label></td> 
       <td><input id="affiliateLinkGenerated" type="text" name="affiliate-link" value="http://firstproduct.html?A="></td> 
      </tr> 
      <tr> 
       <td><label for=""> Second Product Name</label></td> 
       <td><input id="affiliateLinkGenerated" type="text" name="affiliate-link" value="http://secondproduct.html?A="></td> 
      </tr> 
      <tr> 
       <td><label for=""> FIrst Product Name</label></td> 
       <td><input id="affiliateLinkGenerated" type="text" name="affiliate-link" value="http://thirdproduct.html?A="></td> 
      </tr> 
      <tr> 
       <td><label for=""> FIrst Product Name</label></td> 
       <td><input id="affiliateLinkGenerated" type="text" name="affiliate-link" value="http://fourthproduct.html?A="></td> 
      </tr> 
      <tr> 
       <td><label for=""> FIrst Product Name</label></td> 
       <td><input id="affiliateLinkGenerated" type="text" name="affiliate-link" value="http://fifthproduct.html?A="></td> 
      </tr> 
      <tr> 
       <td><label for=""> FIrst Product Name</label></td> 
       <td><input id="affiliateLinkGenerated" type="text" name="affiliate-link" value="http://sixthproduct.html?A="></td> 
      </tr> 
      <tr> 
       <td><label for=""> FIrst Product Name</label></td> 
       <td><input id="affiliateLinkGenerated" type="text" name="affiliate-link" value="http://seventhproduct.html?A="></td> 
      </tr> 
     </tbody> 
    </table> 
<div > 
+0

'元素的id'在'document'应该是唯一的。在''元素处替代'class =“affiliateLinkGenerated”'作为'id =“affiliateLinkGenerated”'' – guest271314

回答

1

我想你想不只是追加而且每次用这个假设我对你的HTML添加一个data attribute每个输入data-link="http://firstproduct.html?A="更换"A="左右。这样你可以很容易做到。

$('#btnGenerateLinks').on('click', function() { 
 
    var valNeed = $('#affiliateID').val(); 
 

 
    // if (valNeed.trim().length) { // In case you want filter blank string 
 
    $('input[name="affiliate-link"]').each(function() { 
 
     $(this).val($(this).data('link') + valNeed); 
 
    }) 
 
// } 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="affiliate-id-form"> 
 
    <input type="text" name="affiliateID" id="affiliateID" placeholder="Enter your affiliate ID eg. 124531" /> 
 
    <input type="button" name="generate-links" id="btnGenerateLinks" value="Generate Links"> 
 
</div> 
 

 

 
<div class="affiliate-links"> 
 
    <table class="affiliateLinksTable"> 
 
    <tbody> 
 
     <tr> 
 
     <td> 
 
      <label for="">FIrst Product Name</label> 
 
     </td> 
 
     <td> 
 
      <input id="affiliateLinkGenerated" type="text" data-link="http://firstproduct.html?A=" name="affiliate-link" value="http://firstproduct.html?A="> 
 
     </td> 
 
     </tr> 
 
     <tr> 
 
     <td> 
 
      <label for="">Second Product Name</label> 
 
     </td> 
 
     <td> 
 
      <input id="affiliateLinkGenerated" type="text" data-link="http://secondproduct.html?A=" name="affiliate-link" value="http://secondproduct.html?A="> 
 
     </td> 
 
     </tr> 
 
     <tr> 
 
     <td> 
 
      <label for="">FIrst Product Name</label> 
 
     </td> 
 
     <td> 
 
      <input id="affiliateLinkGenerated" type="text" data-link="http://thirdproduct.html?A=" name="affiliate-link" value="http://thirdproduct.html?A="> 
 
     </td> 
 
     </tr> 
 
     <tr> 
 
     <td> 
 
      <label for="">FIrst Product Name</label> 
 
     </td> 
 
     <td> 
 
      <input id="affiliateLinkGenerated" type="text" data-link="http://fourthproduct.html?A=" name="affiliate-link" value="http://fourthproduct.html?A="> 
 
     </td> 
 
     </tr> 
 
     <tr> 
 
     <td> 
 
      <label for="">FIrst Product Name</label> 
 
     </td> 
 
     <td> 
 
      <input id="affiliateLinkGenerated" type="text" data-link="http://fifthproduct.html?A=" name="affiliate-link" value="http://fifthproduct.html?A="> 
 
     </td> 
 
     </tr> 
 
     <tr> 
 
     <td> 
 
      <label for="">FIrst Product Name</label> 
 
     </td> 
 
     <td> 
 
      <input id="affiliateLinkGenerated" type="text" data-link="http://sixthproduct.html?A=" name="affiliate-link" value="http://sixthproduct.html?A="> 
 
     </td> 
 
     </tr> 
 
     <tr> 
 
     <td> 
 
      <label for="">FIrst Product Name</label> 
 
     </td> 
 
     <td> 
 
      <input id="affiliateLinkGenerated" type="text" data-link="http://seventhproduct.html?A=" name="affiliate-link" value="http://seventhproduct.html?A="> 
 
     </td> 
 
     </tr> 
 
    </tbody> 
 
    </table> 
 
    <div>

0

$("#btnGenerateLinks").on("click", function() { 
 
    $(".myClass").each(function() { 
 
    $(this).val($(this).val() + $("#affiliateID").val()); 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="affiliate-links"> 
 
    <table class="affiliateLinksTable"> 
 
     <tbody> 
 
      <tr> 
 
       <td><label for=""> FIrst Product Name</label></td> 
 
       <td><input class="myClass" id="affiliateLinkGenerated" type="text" name="affiliate-link" value="http://firstproduct.html?A="></td> 
 
      </tr> 
 
      <tr> 
 
       <td><label for=""> Second Product Name</label></td> 
 
       <td><input class="myClass" id="affiliateLinkGenerated" type="text" name="affiliate-link" value="http://secondproduct.html?A="></td> 
 
      </tr> 
 
      <tr> 
 
       <td><label for=""> FIrst Product Name</label></td> 
 
       <td><input class="myClass" id="affiliateLinkGenerated" type="text" name="affiliate-link" value="http://thirdproduct.html?A="></td> 
 
      </tr> 
 
      <tr> 
 
       <td><label for=""> FIrst Product Name</label></td> 
 
       <td><input class="myClass" id="affiliateLinkGenerated" type="text" name="affiliate-link" value="http://fourthproduct.html?A="></td> 
 
      </tr> 
 
      <tr> 
 
       <td><label for=""> FIrst Product Name</label></td> 
 
       <td><input class="myClass" id="affiliateLinkGenerated" type="text" name="affiliate-link" value="http://fifthproduct.html?A="></td> 
 
      </tr> 
 
      <tr> 
 
       <td><label for=""> FIrst Product Name</label></td> 
 
       <td><input class="myClass" id="affiliateLinkGenerated" type="text" name="affiliate-link" value="http://sixthproduct.html?A="></td> 
 
      </tr> 
 
      <tr> 
 
       <td><label for=""> FIrst Product Name</label></td> 
 
       <td><input class="myClass" id="affiliateLinkGenerated" type="text" name="affiliate-link" value="http://seventhproduct.html?A="></td> 
 
      </tr> 
 
     </tbody> 
 
    </table> 
 
<div > 
 
    
 
    <div class="affiliate-id-form"> 
 
    <input type="text" name="affiliateID" id="affiliateID" placeholder="Enter your affiliate ID eg. 124531" /> 
 
    <input type="button" name="generate-links" id="btnGenerateLinks" value="Generate Links"> 
 
</div>

0

元素iddocument应该是唯一的。替代class="affiliateLinkGenerated"id="affiliateLinkGenerated"<input>元素。

click附加事件来​​元件,使用.querySelectorAll()选择".affiliateLinkGenerated"input元件,在for循环迭代元件,设置每个元件的.value到点击<input type="button">元件的.previousElementSibling

document.getElementById("btnGenerateLinks") 
 
.addEventListener("click", function() { 
 
    var prev = this.previousElementSibling; 
 
    var inputs = document.querySelectorAll(".affiliateLinkGenerated"); 
 
    for (var i = 0; i < inputs.length; i++) { 
 
    inputs[i].value = prev.value; 
 
    } 
 
})
<div class="affiliate-id-form"> 
 
    <input type="text" name="affiliateID" id="affiliateID" placeholder="Enter your affiliate ID eg. 124531" /> 
 
    <input type="button" name="generate-links" id="btnGenerateLinks" value="Generate Links"> 
 
</div> 
 

 
<div class="affiliate-links"> 
 
    <table class="affiliateLinksTable"> 
 
    <tbody> 
 
     <tr> 
 
     <td> 
 
      <label for="">FIrst Product Name</label> 
 
     </td> 
 
     <td> 
 
      <input class="affiliateLinkGenerated" type="text" name="affiliate-link" value="http://firstproduct.html?A="> 
 
     </td> 
 
     </tr> 
 
     <tr> 
 
     <td> 
 
      <label for="">Second Product Name</label> 
 
     </td> 
 
     <td> 
 
      <input class="affiliateLinkGenerated" type="text" name="affiliate-link" value="http://secondproduct.html?A="> 
 
     </td> 
 
     </tr> 
 
     <tr> 
 
     <td> 
 
      <label for="">FIrst Product Name</label> 
 
     </td> 
 
     <td> 
 
      <input class="affiliateLinkGenerated" type="text" name="affiliate-link" value="http://thirdproduct.html?A="> 
 
     </td> 
 
     </tr> 
 
     <tr> 
 
     <td> 
 
      <label for="">FIrst Product Name</label> 
 
     </td> 
 
     <td> 
 
      <input class="affiliateLinkGenerated" type="text" name="affiliate-link" value="http://fourthproduct.html?A="> 
 
     </td> 
 
     </tr> 
 
     <tr> 
 
     <td> 
 
      <label for="">FIrst Product Name</label> 
 
     </td> 
 
     <td> 
 
      <input class="affiliateLinkGenerated" type="text" name="affiliate-link" value="http://fifthproduct.html?A="> 
 
     </td> 
 
     </tr> 
 
     <tr> 
 
     <td> 
 
      <label for="">FIrst Product Name</label> 
 
     </td> 
 
     <td> 
 
      <input class="affiliateLinkGenerated" type="text" name="affiliate-link" value="http://sixthproduct.html?A="> 
 
     </td> 
 
     </tr> 
 
     <tr> 
 
     <td> 
 
      <label for="">FIrst Product Name</label> 
 
     </td> 
 
     <td> 
 
      <input class="affiliateLinkGenerated" type="text" name="affiliate-link" value="http://seventhproduct.html?A="> 
 
     </td> 
 
     </tr> 
 
    </tbody> 
 
    </table> 
 
</div>

相关问题