我有一个名为taskButtonClick()的函数:
并用表单替换那些已删除的元素。
function taskButtonClick() {
$(".task-btn").click(function(){
// Setting form to the clone I want to acces in .on()
var form = $(this).closest('#form-to-date').clone(true);
var formParent = $(this).closest('#date-container');
$(this).closest('#form-to-date').remove();
$(formParent).html('\
<form>\
<fieldset>\
<legend>Task<button class="btn task-btn" id="exit- button"><img src="http://localhost/ia/assets/img/remove.png"></button></legend>\
<input type="text" class="span12 input-xlarge" id="input01">\
<div class="control-group">\
<div class="controls">\
</div>\
</div>\
<div class="form-actions">\
<button class="btn btn-primary span12" type="submit">Save changes</button>\
</div>\
</fieldset>\
</form>\
');
});
}
在表单中添加了一个退出按钮,我为其创建了一个.on()函数并附加了一个单击处理程序。我试图找出一种从.on函数中访问变量形式(克隆)的方法,并将表单替换为以前的内容:
$("#date-container").on("click", "#exit-button", function(event){
alert("Exit Button Works.");
event.preventDefault();
// Here's kind of what I'm going for
$(form).appendTo(formParent);
});
我对jquery不是很熟悉,所以如果我能以某种方式更好地澄清,请告诉我。
Edit: Here's where I'm calling the funcitons:
$('document').ready(function(){
setAjax();
initialize();
setDates();
setButton();
setActiveNav();
taskButtonClick();
removeButtonClicked();
});
答案 0 :(得分:1)
尝试声明一个全局变量。
$(document).ready(function(){
//Notice variable is outside of the event function. It can be accessed by other functions and events.
var formParent = null;
$(".task-btn").click(function(){
// Setting form to the clone I want to acces in .on()
var form = $(this).closest('#form-to-date').clone(true);
formParent = $(this).closest('#date-container');
$(this).closest('#form-to-date').remove();
$(formParent).html('\
<form>\
<fieldset>\
<legend>Task<button class="btn task-btn" id="exit- button"><img src="http://localhost/ia/assets/img/remove.png"></button></legend>\
<input type="text" class="span12 input-xlarge" id="input01">\
<div class="control-group">\
<div class="controls">\
</div>\
</div>\
<div class="form-actions">\
<button class="btn btn-primary span12" type="submit">Save changes</button>\
</div>\
</fieldset>\
</form>\
');
});
});
答案 1 :(得分:0)
如果要使用clone变量,请使用全局变量声明。