[英]How to add name attribute of input field dynamically using Javascript/Jquery
I need one help.I need to add input field name attribute dynamically using Javascript/Jquery. 我需要一个帮助。我需要使用Javascript / Jquery动态添加输入字段名称属性。 I am each time creating new field by clicking on
+
button and delete the required field by clicking on -
button.I am explaining my code below. 我每次都通过单击
+
按钮创建新字段,并通过单击-
按钮删除必填字段。我在下面解释我的代码。
function createNew(evt){ $('#questionshowp1').append('<div class="form-group"><input name="questions1" id="questions1" class="form-control firstsec" placeholder="Text, Image URL, or LaTeX" value="" type="text"><div class="secondsec"><button type="button" class="btn btn-sm btn-success exp-add" style="line-height:12px;" onclick="createNew(this);"><i class="fa fa-plus" aria-hidden="true"></i></button><button type="button" class="btn btn-sm btn-danger exp-minus" style="line-height:12px;" onclick="deleteNew(this)"><i class="fa fa-minus" aria-hidden="true"></i></button></div></div>'); $(evt).css('display', 'none'); $(evt).siblings("button.btn-danger").css('display', 'block'); } function deleteNew(evnt){ $(evnt).closest(".form-group").remove(); }
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/> <div class="questionshowp" id="questionshowp1"> <div class="form-group"> <input name="questions0" id="questions0" class="form-control firstsec" placeholder="Text, Image URL, or LaTeX" value="" type="text"> <div class="secondsec"> <button type="button" class="btn btn-sm btn-success exp-add" style="line-height:12px;" onclick="createNew(this);"><i class="fa fa-plus" aria-hidden="true"></i></button> <button type="button" class="btn btn-sm btn-danger exp-minus" style="line-height:12px;display:none;" onclick="deleteNew(this)"><i class="fa fa-minus" aria-hidden="true"></i></button> </div> </div> </div>
Here i am creating new field using +
button and here i need to increment the name attribute value like questions0,questions1,questions2....
this.Suppose user delete any field then also this given order will remain with all field as name attribute value.Please help me. 在这里,我正在使用
+
按钮创建新字段,在这里,我需要增加名称属性值,例如questions0,questions1,questions2....
this。假设用户删除了任何字段,那么这个给定的顺序将保留所有字段作为名称属性值。请帮我。
First of all, your aproach is pretty bad. 首先,您的方法很糟糕。 It's not a good idea to put everything in a long string and place it somewhere in your JS.
将所有内容放在长字符串中并将其放置在JS中不是一个好主意。 But, this is not your question.
但是,这不是您的问题。
Just create a new variable in your script for counting. 只需在脚本中创建一个新变量进行计数即可。
var count = 0;
And then increase the count in your createNew
function and place the value everywhere you need it in your element string. 然后增加
createNew
函数中的计数,并将该值放置在元素字符串中所需的任何位置。
++count;
'<input name="questions' + count + '" id="questions' + count + '"'
That's it. 而已。
Have a look attached snippet. 看看附带的代码段。
function createNew(evt){ var cur=$("input:text").length+1; $('#questionshowp1').append('<div class="form-group"><input name="questions'+cur+'" id="questions1" class="form-control firstsec" placeholder="Text, Image URL, or LaTeX" value="" type="text"><div class="secondsec"><button type="button" class="btn btn-sm btn-success exp-add" style="line-height:12px;" onclick="createNew(this);">+<i class="fa fa-plus" aria-hidden="true"></i></button><button type="button" class="btn btn-sm btn-danger exp-minus" style="line-height:12px;" onclick="deleteNew(this)">-<i class="fa fa-minus" aria-hidden="true"></i></button></div></div>'); $(evt).css('display', 'none'); $(evt).siblings("button.btn-danger").css('display', 'block'); var i=0; $('input[type="text"]').each(function(){ $(this).attr('name', 'questions' + i); $(this).attr('id', 'questions' + i); i++; }); } function deleteNew(evnt){ $(evnt).closest(".form-group").remove(); var i=0; $('input[type="text"]').each(function(){ $(this).attr('name', 'questions' + i); $(this).attr('id', 'questions' + i); i++; }); }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="questionshowp" id="questionshowp1"> <div class="form-group"> <input name="questions0" id="questions0" class="form-control firstsec" placeholder="Text, Image URL, or LaTeX" value="" type="text"> <div class="secondsec"> <button type="button" class="btn btn-sm btn-success exp-add" style="line-height:12px;" onclick="createNew(this);">+<i class="fa fa-plus" aria-hidden="true"></i></button> <button type="button" class="btn btn-sm btn-danger exp-minus" style="line-height:12px;display:none;" onclick="deleteNew(this)">-<i class="fa fa-minus" aria-hidden="true"></i></button> </div> </div> </div>
You simply have to count
the number of clicks and use that count in your script to have unique name attributes. 您只需要
count
点击次数,并在脚本中使用该计数即可获得唯一的名称属性。
<script type="text/javascript">
var clicks = 0;
function createNew() {
clicks += 1;
var ques = "questions" + clicks;
$('#questionshowp1').append('<div class="form-group"><input name='+ques+' id="questions1" class="form-control firstsec" placeholder="Text, Image URL, or LaTeX" value="" type="text"><div class="secondsec"><button type="button" class="btn btn-sm btn-success exp-add" style="line-height:12px;" onclick="createNew(this);"><i class="fa fa-plus" aria-hidden="true"></i></button><button type="button" class="btn btn-sm btn-danger exp-minus" style="line-height:12px;" onclick="deleteNew(this)"><i class="fa fa-minus" aria-hidden="true"></i></button></div></div>');
$(evt).css('display', 'none');
$(evt).siblings("button.btn-danger").css('display', 'block');
};
</script>
Create a global variable idCount in your script and in createNew
function increment its value. 在脚本中创建一个全局变量idCount ,并在
createNew
函数中增加其值。
var idCount = 0; function createNew(evt){ idCount++; $('#questionshowp1').append('<div class="form-group"><input name="questions'+idCount+'" id="questions1" class="form-control firstsec" placeholder="Text, Image URL, or LaTeX" value="" type="text"><div class="secondsec"><button type="button" class="btn btn-sm btn-success exp-add" style="line-height:12px;" onclick="createNew(this);">+<i class="fa fa-plus" aria-hidden="true"></i></button><button type="button" class="btn btn-sm btn-danger exp-minus" style="line-height:12px;" onclick="deleteNew(this)">-<i class="fa fa-minus" aria-hidden="true"></i></button></div></div>'); $(evt).css('display', 'none'); $(evt).siblings("button.btn-danger").css('display', 'block'); } function deleteNew(evnt){ $(evnt).closest(".form-group").remove(); }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="questionshowp" id="questionshowp1"> <div class="form-group"> <input name="questions0" id="questions0" class="form-control firstsec" placeholder="Text, Image URL, or LaTeX" value="" type="text"> <div class="secondsec"> <button type="button" class="btn btn-sm btn-success exp-add" style="line-height:12px;" onclick="createNew(this);">+<i class="fa fa-plus" aria-hidden="true"></i></button> <button type="button" class="btn btn-sm btn-danger exp-minus" style="line-height:12px;display:none;" onclick="deleteNew(this)">-<i class="fa fa-minus" aria-hidden="true"></i></button> </div> </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.