简体   繁体   English

如何将javascript函数应用于多个同名输入字段

[英]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输入-更改为类,一切都很好-请参见以下代码段:

Updated Fiddle 更新小提琴

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.

相关问题 JQUERY:在多个输入字段上应用相同的功能 - JQUERY: Apply same function on multiple Input fields 如何在 Javascript / jQuery 中将函数应用于多个具有相同名称的元素 - how to apply a function to multiple elements with the same name in Javascript / jQuery 如何使用JavaScript使用相同的功能对多个输入字段进行数据验证? - How can I use the same function for data validation of multiple input fields using JavaScript? 使用相同名称定位多个输入字段 - Targeting multiple input fields with using same name jQuery选择器用于具有相同名称的多个输入字段 - Jquery selector for multiple input fields with the same name javascript如何将多个表单字段的更改绑定到同一函数中? - javascript how to bind changes of multiple form fields into the same function? 如何将具有相同类和名称的多个输入字段传递给ajax请求并根据回复更改相应的字段? - How to pass multiple input fields having same class and name to ajax request and change corresponding fields based on reply? javascript-如何验证具有多个相同名称的动态添加输入? - javascript - how to validate dynamic add input with multiple same name? JQUERY:如何序列化具有相同名称的输入字段 - JQUERY: how to serialize input fields with same name 使用jQuery的具有相同名称但不同ID的多个输入字段的总和 - Sum of multiple input fields with same name but not same id using jQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM