JQuery Append Html onclick function
JQuery Append Html onclick function
我试图均匀地删除文本框。我的意思是,对于每个文本框,我想要删除按钮。如果我点击删除按钮,我想删除文本框
用于添加多个文本框的Html<pre><div id="TextBoxDiv1">
<label style="float: none;"> Bus Model
<input style="width: 150px;" type="text" value="" class="validate[required,custom[onlyNumberSp]]" name="numsValid" id="numsValid">
<img src="images/india_rupess.png" style="margin-left:18px;"> :</label>
<input style="width: 150px;" type="text" value="" class="validate[required,custom[onlyNumberSp]]" name="numsValid" id="numsValid">
<label style="float: none;margin-left: 16px;"><span class="font-dollar">﹩</span> :</label>
<input style="width: 150px;" type="text" value="" class="validate[required,custom[onlyNumberSp]]" name="numsValid" id="numsValid">
<a href="javascript:;" id="addButton">Add</a> <a href="javascript:;" id="removeButton">Remove</a>
</div></pre>
这是我的脚本
<pre>var counter = 2;
$("#addButton").click(function () {
$("#TextBoxesGroup").append('<div id="TextBoxDiv' + counter + '" class="textadd""><label style="float: none;"> Bus Model <input style="width: 150px;margin-left:3px;" type="text" value="" class="validate[required,custom[onlyNumberSp]]" name="bus' + counter + '" id="numsValid"><img src="images/india_rupess.png" style="margin-left:21px;"> :</label><input style="width: 150px;margin-left:3px;" type="text" value="" class="validate[required,custom[onlyNumberSp]]" name="rs' + counter + '" id="numsValid"><label style="float: none;margin-left: 19px;"><span class="font-dollar">﹩</span> :</label><input style="width: 150px;margin-left:2px;" type="text" value="" class="validate[required,custom[onlyNumberSp]]" name="dollar' + counter + '" id="numsValid"> <a href="javascript:;" onClick="javascript:alert("test");" >Remove</a></div>');
counter++;;
});
点击查看http://jsfiddle.net/b8DRT/
您可以在删除按钮上添加一个类,并在$("#TextBoxesGroup")
上使用.on()
添加remove事件。这样的
$("#TextBoxesGroup").on('click', '.removeButton', function(){
$(this).closest('div').remove();
});
id
对于完整文档中的HTML元素是唯一的。你将相同的id
给予多个元素。其次,当您使用click
附加事件处理程序时,它们绑定到DOM中已经存在的元素,而不是稍后要添加的元素。如果你想附加点击事件处理程序,你应该使用on
方法,如下所示:
$(document).on('click', '.removeButton', function(e){
// your logic here...
});
相关文章:
- 直接在函数声明上使用function.prototype.bind
- Javascript onClick and mouseOver function
- Image OnClick to Javascript function
- Javascript get "this" class with onClick function
- 为什么当我尝试在节点中运行函数时,Onclick=“function()” 会显示 404
- on(“click”,function())和onclick=“”之间有什么区别;函数()&”;
- checkbox onclick function in another function
- javascript onclick function null
- onclick =“函数()“;不工作但href="javascript: function()"工
- onclick = "javascript: javascript: function(value);&quo
- 试图通过onclick function()括号传递不同的变量
- JQuery Append Html onclick function
- jquery中的onclick(function(){})和on('click',function(){
- onclick never execute function
- Control onClick function
- 防止onclick-function在settimeout函数中被激活
- 从onclick=function()获取按钮的内容
- OnClick不工作时,我做OnClientClick="function();返回false"
- Javascript 在 onclick 事件中无法识别 self.function
- Javascript function onclick