简体   繁体   中英

how to preserve element after button is clicked

i was trying to add element while button is clicked.but it is added when i click on button and it is gone just in 2 minutes.so how to set permenantly.following is code i tried.

$(document).ready(function() {
  var d = "";
  $("input[name='submit']").click(function() {
    alert("button is clicked");
    d = $("select").val();
    alert("d====" + d);
    myfunction(d);
  });

  function myfunction(d) {
    if (d != "" && d != "undefined" && d != null) {
      for (i = 0; i < d; i++) {
        alert("hello");
        var d1 = "<div class='form-group'><label>Upload Image</label><div class='input-group'><span class='input-group-addon'><i class='fa fa-picture-o'></i></span><input type='file' name=" + i + "class='form-control' placeholder='Upload your photo' id='img'></div></div>";
        $("#d2").append(d1);
      }
    }
  }
});

抱歉,我将提交按钮放在表单元素中,同时单击按钮,这是帖子,页面已加载(刷新)并且内容被禁用....这就是原因。

is your Submit button placed in a FORM tag?

i think it happens because when you click on a Submit button, your page redirects you to Target url

change your code like below:

 $(document).ready(function() { var d = ""; $("input[name='submit']").click(function(e) { e.preventDefault(); alert("button is clicked"); d = $("select").val(); alert("d====" + d); myfunction(d); }); function myfunction(d) { if (d != "" && d != "undefined" && d != null) { for (i = 0; i < d; i++) { alert("hello"); var d1 = "<div class='form-group'><label>Upload Image</label><div class='input-group'><span class='input-group-addon'><i class='fa fa-picture-o'></i></span><input type='file' name=" + i + "class='form-control' placeholder='Upload your photo' id='img'></div></div>"; $("#d2").append(d1); } } } }); 

place this code inside document.ready block

$('form').submit(function ()
{
    return false;
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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