简体   繁体   English

如何添加在页面刷新时不会消失的动态输入字段?

[英]How to add dynamic input field that does not disappear on page refresh?

I want to add input fields, as well as remove, on clicking a button. 我想在单击按钮时添加输入字段,以及删除输入字段。

I can add input boxes using jQuery, but I don't know the way to make it permanent. 我可以使用jQuery添加输入框,但是我不知道使它永久化的方法。 Do I need to save it in the database to make it permanent? 我需要将其保存在数据库中以使其永久吗? I am using the following code with a button: 我使用带有按钮的以下代码:

<div class="input_fields_container">
  <div><input type="text" name="product_name[]">
    <button class="btn btn-sm btn-primary add_more_button">Add More Fields</button>
  </div>
</div>

And the jQuery is as follows: jQuery如下所示:

<script>
$(document).ready(function () {
var max_fields_limit = 10; // Set limit for maximum input fields
    var x = 1; // Initialize counter for text box
    $('.add_more_button').click(function (e) { // Click event to add more fields
        e.preventDefault();
        if (x < max_fields_limit) { // Check conditions
            x++; // Increment counter
            $('.input_fields_container').append('<div><input type="text" name="product_name[]"/><a href="#" class="remove_field" style="margin-left:10px;">Remove</a></div>'); // Add input field
        }
    });  
    $('.input_fields_container').on("click", ".remove_field", function (e) { // User click on remove text links
        e.preventDefault();
        $(this).parent('div').remove();
        x--;
    })
});
</script>

How do I make it dynamic, so input field does not disappear on page refresh until I remove it? 如何使其具有动态性,因此input字段在刷新后才消失,直到将其删除?

Use cookie, run below code in your LOCAL and refresh the page. 使用Cookie, 在您本地的代码下运行,然后刷新页面。 See magic. 看魔术。

 $(document).ready(function () { var getControl=getCookie("countControls1"); var max_fields_limit = 10; // Set limit for maximum input fields var x = 0; // Initialize counter for text box for(var i=1; i<=getControl; i++) { console.log(i); addControls(); } $('.add_more_button').click(function (e) { // Click event to add more fields addControls(); }); function addControls() { console.log(x); if (x < max_fields_limit) { x++; // Increment counter setCookie("countControls1",x,100); $('.input_fields_container').append('<div><input type="text" id="id'+x+'" name="product_name[]"/><a href="#" class="remove_field" style="margin-left:10px;">Remove</a></div>'); // Add input field } } $('.input_fields_container').on("click", ".remove_field", function (e) { // User click on remove text links $(this).parent('div').remove(); x--; setCookie("countControls1",x,100); }); function setCookie(cname,cvalue,exdays) { var d = new Date(); d.setTime(d.getTime() + (exdays*24*60*60*1000)); var expires = "expires=" + d.toGMTString(); document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/"; $("#div1").html("Add cookie = ",getCookie(cname)); } function getCookie(cname) { console.clear(); var name = cname + "="; var decodedCookie = decodeURIComponent(document.cookie); var ca = decodedCookie.split(';'); //console.log(cname); //console.log(decodedCookie); console.log(ca); for(var i = 0; i < ca.length; i++) { var c = ca[i]; while (c.charAt(0) == ' ') { c = c.substring(1); } if (c.indexOf(name) == 0) { console.log(c.substring(name.length, c.length)); return (c.substring(name.length, c.length)); } } return ""; } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <input type="button" class="add_more_button" value="Add" /> <div class="input_fields_container"></div> 

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

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