禁用jquery移动表单按钮操作
可能重复:禁用jquery移动表单按钮操作,jquery,jquery-mobile,Jquery,Jquery Mobile,可能重复: 我有一个表单,我使用jQuery来检查和验证表单,这样,如果表单为空,它就不会提交表单,只会提醒“请填写所有字段” 现在的问题是,每当我点击submit按钮时,我都会被重定向到页面(应该是用户面板),只会显示一条文字: 未定义 如何禁用jQuery按钮在默认情况下的功能 编辑: My event(我的事件)激发,消息显示,但我仍然被重定向到表单属性action中定义的URL。在验证表单字段的函数中(应在提交事件中调用),如果验证成功,则需要返回true,和false否则 这样,当验
我有一个表单,我使用jQuery来检查和验证表单,这样,如果表单为空,它就不会提交表单,只会提醒“请填写所有字段” 现在的问题是,每当我点击submit按钮时,我都会被重定向到页面(应该是用户面板),只会显示一条文字: 未定义 如何禁用jQuery按钮在默认情况下的功能 编辑:
My event(我的事件)激发,消息显示,但我仍然被重定向到表单属性
action
中定义的URL。在验证表单字段的函数中(应在提交
事件中调用),如果验证成功,则需要返回true
,和false
否则
这样,当验证失败时,您可以防止表单导航到操作
属性中定义的URL
您可以尝试以下方法:
$(function() {
$("#your_form").submit(function() {
// Define `sField`
var sField = // SOMETHING
// Define `iInd`
var iInd = // SOMETHING
// Define `sError`
var sError = // SOMETHING
return doShowError($(this), sField, iInd, sError);
});
});
function doShowError( eForm, sField, iInd, sError ) {
// `haserror`: boolean output which will equal `true`
// if all the fields are correct, and `else` otherwise
var haserror = true;
var $Field = $( "[name='" + sField + "']", eForm ); // single (system) field
if( !$Field.length ) { // couple field
$Field = $( "[name='" + sField + '[' + iInd + ']' + "']", eForm );
haserror = false;
}
if( !$Field.length ) { // couple multi-select
$Field = $( "[name='" + sField + '[' + iInd + '][]' + "']", eForm );
haserror = false;
}
if( !$Field.length ) { // couple range (two fields)
$Field = $( "[name='" + sField + '[' + iInd + '][0]' + "'],[name='" + sField + '[' + iInd + '][1]' + "']", eForm );
haserror = false;
}
//alert( sField + ' ' + $Field.length );
$Field.parents('div:first').addClass( 'error' );
$Field.parents('div:first')
.addClass( 'error' )
.children( 'img.warn' )
.attr('float_info', sError)
//.show()
;
return haserror;
}
其中,#your_form
是表单的ID,doShowError()
是验证表单输入的函数,如果所有字段都正常,则返回true
,反之则返回else
在这种情况下,您可能需要修改函数doShowError
,使其返回如上所述的true
或false
。您可以将其更改为以下内容:
$(function() {
$("#your_form").submit(function() {
// Define `sField`
var sField = // SOMETHING
// Define `iInd`
var iInd = // SOMETHING
// Define `sError`
var sError = // SOMETHING
return doShowError($(this), sField, iInd, sError);
});
});
function doShowError( eForm, sField, iInd, sError ) {
// `haserror`: boolean output which will equal `true`
// if all the fields are correct, and `else` otherwise
var haserror = true;
var $Field = $( "[name='" + sField + "']", eForm ); // single (system) field
if( !$Field.length ) { // couple field
$Field = $( "[name='" + sField + '[' + iInd + ']' + "']", eForm );
haserror = false;
}
if( !$Field.length ) { // couple multi-select
$Field = $( "[name='" + sField + '[' + iInd + '][]' + "']", eForm );
haserror = false;
}
if( !$Field.length ) { // couple range (two fields)
$Field = $( "[name='" + sField + '[' + iInd + '][0]' + "'],[name='" + sField + '[' + iInd + '][1]' + "']", eForm );
haserror = false;
}
//alert( sField + ' ' + $Field.length );
$Field.parents('div:first').addClass( 'error' );
$Field.parents('div:first')
.addClass( 'error' )
.children( 'img.warn' )
.attr('float_info', sError)
//.show()
;
return haserror;
}
你好你在哪里调用你的函数
doShowError
?你能提供更多的代码吗?谢谢你的回答,但仍然面临同样的问题。。我的代码应该做的是保持在同一个页面上,只是提醒错误,但现在我进入了操作页面,不仅是它,而且它只有未定义的