简体   繁体   English

使用jquery在单击时将html输入框添加到div

[英]Add html input boxes to a div on click using jquery

I need to add input boxes to a page onclick of a button, I have written the following jquery script. 我需要在一个按钮的界面上添加输入框,我已经编写了以下jquery脚本。 It adds one input box when clicked the first time but does not repeat adding. 它在第一次单击时添加一个输入框但不重复添加。

<script type="text/javascript">
   function addInput(){
      $('#fileinput').html('<label>Filename:</label>
                        <input type="file" name="file"  id="file" />');
   }
</script>

can some one please help me to modify the code so that it will add an input box each time the button is clicked. 有人可以帮我修改代码,这样每次点击按钮时都会添加一个输入框。

$('#buttonID').click(function(){
    $('#fileinput').html($('#fileinput').html()+'<label>Filename:</label>
                    <input type="file" name="file"  id="file" />');
});

or use append as others suggested 或使用附加建议的其他人

$('#buttonID').click(function(){
    $('#fileinput').append('<label>Filename:</label>
                    <input type="file" name="file"  id="file" />');
});

Your code currently replaces the existing HTML in your #fileInput element with the same HTML. 您的代码目前使用相同的HTML替换#fileInput元素中的现有HTML。 What you probably want to use is the append() method. 你可能想要使用的是append()方法。

   function addInput(){
      $('#fileinput').append('<label>Filename:</label>
                        <input type="file" name="file"  id="file" />');
   }

那是因为你不是每次都添加 HTML,你只是将它设置为显示单个input

try using append instead of html : http://api.jquery.com/append/ 尝试使用append而不是htmlhttp//api.jquery.com/append/

<script type="text/javascript">
 function addInput(){
  $('#fileinput').append('<label>Filename:</label>
              <input type="file" name="file"  id="file" />');
}
</script>

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

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