我正在处理一个表单,在该表单中我想使用按钮向该表单添加功能,更改按钮的属性,然后使用删除那些相同的元素。我已经开始工作,可以添加元素和功能并更改按钮的属性,但是我无法删除其中的添加元素。下面是带有表单的测试代码。最后,我想添加元素以使用相同的按钮添加表单,提交数据然后删除元素来更新数据库。
$(function(){
$("#addform").click(function(){
var adddiv = $("<div>").attr('id','addediv').addClass("incident").append(
$("<label>").addClass("title").text("TO BE COMPLETED BY THE SUPERVISOR"),
$("<div>").append(
$("<input>").attr("type", "text").attr("id", "incidate").addClass("datepick")
)
);
$(".incident").remove();
$("form").append(adddiv);
$(function(){
$(".datepick").datepicker({
changeMonth: true,
changeYear: true
});
}
);
$("#addform").attr('value','Remove Form').attr('id','removeForm');
});
});
$(function(){
$("#removeForm").click(function(){
$("#addediv").remove();
})
})
</script>
<form>
<input type="button" id="addform" value="Add form" />
</form>
您似乎使过程变得过于复杂。如果您有两个按钮和两个事件处理程序,它将更容易阅读。
HTML:
<input type="button" id="btnAdd" Value="Add"/>
<input type="button" id="btnRemove" Value="Remove" />
jQuery的:
$("#btnAdd").click(function(){
$(this).css("display", "none");
$("#btnRemove").css("display", "inline-block");
//Add your stuff here
});
$("#btnRemove").click(function(){
$(this).css("display", "none");
$("#btnAdd").css("display", "inline-block");
//Remove your stuff here
});
您的问题似乎是由于您的函数未使用“ on”或“ live”引起的,这意味着绑定是在页面加载时发生的。由于“ btnRemove”当时不存在,因此事件处理程序永远不会真正绑定到它。当然,您可以在更改按钮的值和id之后添加绑定,但这不必要地很复杂,因此我认为上述解决方案更好,更易读。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句