简体   繁体   English

使用 jquery 将函数动态添加到动态表单元素

[英]Dynamically add function to dynamic form elements using jquery

I've created a webpage with dynamic form elements using jquery.我使用 jquery 创建了一个带有动态表单元素的网页。 Now I want to apply same functions to the dynamically added elements.现在我想对动态添加的元素应用相同的功能。 How to do it?怎么做?

Check this: https://jsfiddle.net/tz5w6fu4/1/检查这个: https : //jsfiddle.net/tz5w6fu4/1/

Here you can see that upon changing text of first text input field, there'll be new option which will be added to the select element next to it and then upon clicking / changing option of the select element, the text input will be changed in the text input field next to that.在这里您可以看到,在更改第一个文本输入字段的文本时,将有一个新选项添加​​到它旁边的选择元素中,然后在单击/更改选择元素的选项时,文本输入将更改为旁边的文本输入字段。 This is happening normally for the first element..这通常发生在第一个元素上。

Now how can I add the same functionality to the the dynamically added form elements?现在如何向动态添加的表单元素添加相同的功能?

You cannot use same id for mutliple elements instead change them to classes .您不能对多个元素使用相同的id 而是将它们更改为 classes 。 Then , using .find() and closest() get reference of the required element and change values there .然后,使用.find()closest()获取所需元素的引用并更改那里的值。

Demo Code :演示代码

 $(document).on('change', '.abc', function() { var get_Select_reference = $(this).closest(".inputFormRow") //get closet div.. get_Select_reference.find(".xyz").empty() //empty your select-box get_Select_reference.find(".def").val("") //empty input if ($(this).val() != "") { get_Select_reference.find(".xyz").append(new Option("Demo", "demo")); get_Select_reference.find(".xyz").append(new Option("Demo1", "demo1")); //append options.. } }); $(document).on('change', '.xyz', function() { var get_input_reference = $(this).closest(".inputFormRow").find(".def") //get input.. var val = $(this).val(); //your select-box value get_input_reference.val(val); }); // add row $("#addRow").click(function() { var html = ''; //added classes.. html += '<div class="inputFormRow">'; html += '<div class="input-group mb-3">'; html += '<input type="text" name="title[]" class="form-control m-input abc" placeholder="Enter title" autocomplete="off">'; html += '<select name="ch[]" class="xyz">'; html += '</select>'; html += '<input type="text" name="name[]" class="form-control m-inpu t def">'; html += '<div class="input-group-append">'; html += '<button type="button" class="btn btn-danger removeRow">Remove</button>'; html += '</div>'; html += '</div>'; $('#newRow').append(html); }); //use class here as well $(document).on('click', '.removeRow', function() { $(this).closest('.inputFormRow').remove(); });
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script> <div class="container" style="max-width: 700px;"> <form method="post" action=""> <div class="row"> <div class="col-lg-12"> <!--added class--> <div class="inputFormRow"> <div class="input-group mb-3"> <input type="text" name="title[]" class="form-control m-input abc" id="abc" placeholder="Enter title" autocomplete="off"> <select name="ch[]" class="xyz" id="xyz"> </select> <input type="text" name="name[]" class="form-control m-input def" id="def"> <div class="input-group-append"> <button id="removeRow" type="button" class="btn btn-danger removeRow">Remove</button> </div> </div> </div> <div id="newRow"></div> <button id="addRow" type="button" class="btn btn-info">Add Row</button> </div> </div> </form> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM