[英]How to apply javascript function into multiple same name input fields
Please help i want to apply the javascript in the input list. 请帮助我要在输入列表中应用javascript。 This is what i have right now. 这就是我现在所拥有的。 When i click the item in select list, the input textbox will be autofill and it is working right now but when i add input textbox using jquery, the javascript i used in select list is not applied on the new input. 当我单击选择列表中的项目时,输入文本框将自动填充,并且现在可以正常工作,但是当我使用jquery添加输入文本框时,我在选择列表中使用的javascript不会应用于新输入。 It apply only on the first select, the original list. 它仅适用于最初选择的原始列表。 What should i do to apply it on all select list? 我应该怎么做才能将其应用于所有选择列表? Click this link for the sample 单击此链接获取示例
$(document).ready(function() {
$("#category").change(function () {
var unit = $(this).find(':selected').data('number');
$('#numbervalue').val(unit);
});
var counter = 2;
$("#addNumber").click(function () {
var newTextBoxDiv = $(document.createElement('tr')).attr("id", 'TextBoxDivMat' + counter);
newTextBoxDiv.after().html('<td><select id="category"><option data-number="1" value="One">One</option><option data-number="2" value="Two">Two</option><option data-number="3" value="Three">Three</option></select></td><td><input id="numbervalue" type="text"></td>');
newTextBoxDiv.appendTo("#TextBoxesGroupMat");
counter++;
});
$("#removeButtonMat").click(function () {
counter--;
$("#TextBoxDivMat" + counter).remove();
});
});
<table id="TextBoxesGroupMat">
<thead>
<tr>
<th>Word</th>
<th>Number</th>
</tr>
</thead>
<tbody id="TextBoxDivMat1">
<tr>
<td>
<select id="category">
<option data-number="1" value="One">One</option>
<option data-number="2" value="Two">Two</option>
<option data-number="3" value="Three">Three</option>
</select>
</td>
<td><input id="numbervalue" type="text"></td>
</tr>
</tbody>
</table>
<button id="addNumber">+ add number</button>
<button id="removeButtonMat">remove</button>
If you add new elements into your HTML document then you need to attach the event to the newly added elements once again. 如果将新元素添加到HTML文档中,则需要再次将事件附加到新添加的元素上。 After you add the new elements, you can call the function. 添加新元素后,可以调用该函数。 But when you use ID then it is very likely that the jQuery will attach the function to the first element having that ID. 但是当您使用ID时,jQuery很可能会将函数附加到具有该ID的第一个元素上。 If you want to use the same function to a bunch of elements then you can use a class identifier. 如果要对一堆元素使用相同的功能,则可以使用类标识符。
// Using ID
$("#category").on("change", function() {
// Your code here
})
// using class
$(".category").on("change", function() {
// your code here
})`
Ok, the problem is that you're changing an ID, which then only changes the first ID input - change to classes and all is good - see below snippet: 好的,问题是您要更改一个ID,然后仅更改第一个ID输入-更改为类,一切都很好-请参见以下代码段:
Also, it's a bad idea to have on change
on a select if there is no blank option - a user must first select two
to then go back and select one
同样,如果没有空白选项,则对选择on change
也是一个坏主意-用户必须先选择two
然后再返回并选择one
$(document).ready(function() { $(document).on("change", ".category", function () { /* changed to class */ var unit = $(this).find(':selected').data('number'); $(this).parent().next().find('.numbervalue').val(unit); /* ugly but works, could be better */ }); var counter = 2; $("#addNumber").click(function () { var newTextBoxDiv = $(document.createElement('tr')) .attr("id", 'TextBoxDivMat' + counter); /* changed to add select and input with classes not IDs */ newTextBoxDiv.after().html('<td><select class="category"><option data-number="1" value="One">One</option><option data-number="2" value="Two">Two</option><option data-number="3" value="Three">Three</option></select></td><td><input class="numbervalue" type="text"></td>'); newTextBoxDiv.appendTo("#TextBoxesGroupMat"); counter++; }); $("#removeButtonMat").click(function () { counter--; $("#TextBoxDivMat" + counter).remove(); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id="TextBoxesGroupMat"> <thead> <tr> <th>Word</th> <th>Number</th> </tr> </thead> <tbody id="TextBoxDivMat1"> <tr> <td><select class="category"> <option data-number="1" value="One">One</option> <option data-number="2" value="Two">Two</option> <option data-number="3" value="Three">Three</option> </select></td> <td><input class="numbervalue" type="text"></td> </tr> </tbody> </table> <button id="addNumber">+ add number</button> <button id="removeButtonMat">remove</button>
Please try this code 请尝试此代码
$(document).on('change',"#category",function () {
var unit = $(this).find(':selected').data('number');
$(this).parent().parent().find('#numbervalue').val(unit);
});
Replace categroy listener with 将categroy侦听器替换为
$(document).on('change', "#category",function () {
var unit = $(this).find(':selected').data('number');
$(this).parent().parent().find('#numbervalue').val(unit);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.