为什么不是';我的javascript函数没有为此函数定义吗?
这是非常一致的,但firebug显示我的saveForm函数不是从我的“button.save”事件处理程序定义的,但它适用于我的“button.deleteForm”事件处理程序:为什么不是';我的javascript函数没有为此函数定义吗?,javascript,jquery,Javascript,Jquery,这是非常一致的,但firebug显示我的saveForm函数不是从我的“button.save”事件处理程序定义的,但它适用于我的“button.deleteForm”事件处理程序: function saveForm(form) { var $form = form; var url = $form.attr('action'); $.ajax({ type: "POST",
function saveForm(form)
{
var $form = form;
var url = $form.attr('action');
$.ajax({
type: "POST",
enctype: 'mutipart/form-data',
url: url,
data: $form.serialize(), // serializes the form's elements.
success: function(data)
{
// data is the server response.
// change this function to tell the
// user whether their submission
// is correct or what fields have
// bad data.
var response = JSON.parse(data);
return true;
}
});
return false; // avoid to execute the actual submit of the form.
}
// Do not use event handlers like .click(). This is the
// only viable solution for handling events on dynamically
// generated HTML elements. This handles the saving of data
// to the server.
$(document).on('click', 'button.save', function(e){
var $form = $(this).closest('form');
saveForm(form);
});
// This event handler is responsible for deleting data.
// For Joey's job: Please make sure that this calls save
// after the user hits delete. This will save the data in
// the database.
$(document).on('click', 'button.deleteForm', function(e){
// Get the form to update before deleting our embedded form
var $form = $(this).closest('form');
var str = $(this).attr('id');
// Get the table id in review to delete
var deleteForm = str + '_review';
$('table#' + deleteForm).remove();
// Get the collection form id to delete
var idArray = str.split('_');
idArray.pop();
divId = '#' + idArray.join('_');
$(divId).remove();
saveForm($form);
});
您在saveform中遗漏了
$
$(document).on('click', 'button.save', function(e){
var $form = $(this).closest('form');
saveForm($form);
//------^----here
});
您在saveform中遗漏了
$
$(document).on('click', 'button.save', function(e){
var $form = $(this).closest('form');
saveForm($form);
//------^----here
});
你不需要传递
$form
而不是form
吗?不,这其实并不重要,因为js是松散类型的。注意AJAX通常是异步的,所以从成功
返回对你没有任何帮助。我想你会发现它确实重要。储存表格(表格);应该是saveForm($form)@学生先生-动态键入并不意味着您可以在使用变量名的某个位置更改变量名并使其继续工作。您不需要通过$form
而不是form
?不,实际上这并不重要,因为js是松散类型的。注意AJAX通常是异步的,所以从成功返回对您没有任何帮助。我想您会发现这很重要。储存表格(表格);应该是saveForm($form)@学生先生-动态键入并不意味着您可以在使用变量名的某个位置更改变量名并使其继续工作。这是一个问题,但问题是为什么saveForm
未定义。我认为saveForm()
已定义。。但是由于传递的参数不正确,OP在那里出错(这使OP认为,函数未定义)…但是在按钮的情况下,deleteForm
参数是正确的,因此没有错误。。只是一个猜测而已。。我看不出有任何理由不定义该函数。这是一个问题,但问题是为什么saveForm
未定义。我认为saveForm()
已定义。。但是由于传递的参数不正确,OP在那里出错(这使OP认为,函数未定义)…但是在按钮的情况下,deleteForm
参数是正确的,因此没有错误。。只是一个猜测而已。。我看不出有什么理由不定义这个函数。。