简体   繁体   English

如何在不刷新页面的情况下向html表单添加新字段

[英]How to add a new field to html form without refreshing the page

I'm making a form for my site and basically I want to add a new field to the form if the user click on + icon.我正在为我的网站制作一个表单,基本上我想在用户点击+图标时向表单添加一个新字段。

Here's the code:这是代码:

<div class='form-group'>
    <div class='form-group'>
        <label>Product Image 1:</label>
        <input type='file' name='product_img1' class='form-control my-colorpicker1'>
    </div>
</div>
<div class='form-group'>
    <div class='form-group'>
        <label>Product Image 2:</label>
        <input type='file' name='product_img2' class='form-control my-colorpicker1'>
    </div>
</div>
<div class='form-group'>
    <div class='form-group'>
        <label>Product Image 3:</label>
        <input type='file' name='product_img3' class='form-control my-colorpicker1'>
    </div>
</div>

<p>Add more images: <a href='' title='click this icon in order to add more images'><span class='glyphicon glyphicon-plus'></span></a></p> 

So if user click on this little plus sing , another field should be added to the form without refreshing the page.因此,如果用户单击这个小加号,则应在不刷新页面的情况下将另一个字段添加到表单中。 So the code will be this:所以代码将是这样的:

<div class='form-group'>
        <div class='form-group'>
            <label>Product Image 1:</label>
            <input type='file' name='product_img1' class='form-control my-colorpicker1'>
        </div>
    </div>
    <div class='form-group'>
        <div class='form-group'>
            <label>Product Image 2:</label>
            <input type='file' name='product_img2' class='form-control my-colorpicker1'>
        </div>
    </div>
    <div class='form-group'>
        <div class='form-group'>
            <label>Product Image 3:</label>
            <input type='file' name='product_img3' class='form-control my-colorpicker1'>
        </div>
    </div>
    <div class='form-group'>
        <div class='form-group'>
            <label>Product Image 4:</label>
            <input type='file' name='product_img1' class='form-control my-colorpicker1'>
        </div>
    </div>
    <p>Add more images: <a href='' title='click this icon in order to add more images'><span class='glyphicon glyphicon-plus'></span></a></p> 

So because I'm not familiar with jQuery stuff, I asked this question here, hopefully someone helped me with that.所以因为我不熟悉 jQuery 的东西,所以我在这里问了这个问题,希望有人能帮助我。 Thanks!谢谢!

Read jQuery clone and append阅读 jQuery克隆追加

 $('#add').click(function() { $('#clone-field').clone().appendTo('#form'); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="form"> <div id="clone-field"> <input type='file' name='product_img[]'> </div> </div> <br> <button id="add">Add more images</button>

You can create a hidden field that will count the current total fields.您可以创建一个隐藏字段来计算当前的总字段数。 Then use the jquery APPEND function.然后使用jquery APPEND 函数。 see below:见下文:

 $("#addbtn").click(function(){ var next = parseInt($('#counter').val()) + 1; $("#group1").append("<div class='form-group'><div class='form-group'><label>Product Image "+next+":</label><input type='file' name='product_img"+next+"' class='form-control my-colorpicker1'></div></div>"); $('#counter').val(next); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="group1"> <div class='form-group'> <div class='form-group'> <label>Product Image 1:</label> <input type='file' name='product_img1' class='form-control my-colorpicker1'> </div> </div> <div class='form-group'> <div class='form-group'> <label>Product Image 2:</label> <input type='file' name='product_img2' class='form-control my-colorpicker1'> </div> </div> <div class='form-group'> <div class='form-group'> <label>Product Image 3:</label> <input type='file' name='product_img3' class='form-control my-colorpicker1'> </div> </div> </div> <input type="hidden" name="counter" id="counter" value="3"> <p>Add more images: <a href='#' title='click this icon in order to add more images' id="addbtn">test<span class='glyphicon glyphicon-plus'></span></a></p>

I think you should look at the jQuery documentation.我认为您应该查看 jQuery 文档。 Or you should probably see some jQuery tutorial.或者你应该看看一些 jQuery 教程。

In any case, since I know that jQuery can be intimidating at first, here is an updated version that does what you want to achieve: https://codepen.io/adelriosantiago/pen/KZaxdG .无论如何,因为我知道 jQuery 一开始可能会令人生畏,所以这里有一个更新版本,可以满足您的需求: https : //codepen.io/adelriosantiago/pen/KZaxdG

Basically I added the jQuery library and this code snippet:基本上我添加了 jQuery 库和这个代码片段:

$('#extra-q').hide(0); //Hide the extra question

$('#add-more').off().on('click', function() { //Set the plus button click trigger
  $('#extra-q').show(0); //Show the extra question
})

Note that this does not append a new element, it only shows a hidden element.请注意,这不会附加新元素,它只会显示一个隐藏元素。 I think it is better to do so, this way your code in your backend will be easier.我认为这样做会更好,这样您在后端的代码会更容易。

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

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