如何验证表单并在链接点击事件中提交?我的代码如下:
new Element('a',{
'html': "To cart",
'class': 'shop_add',
'href': 'javascript:void(0)',
events: {
click: function(e){
new Form.Validator(product_details_sub_container, {
stopOnFailure: true,
useTitles: true,
errorPrefix: "",
ignoreHidden : false,
onFormValidate: function(passed, form, event) {
console.log("Validation status: " + passed);
if (passed) {
event.stop();
...ajax request is here
}
}
});
}
}
}).inject(container);
为什么onFormValidate()函数不起作用?谢谢!
答案 0 :(得分:0)
您正在每次链接点击时创建新的Form.Validator。 我没有看到整个代码,但这里是您发布的代码的清理版本,并且同时适用于按钮和链接:
<强>的JavaScript 强>
window.addEvent('domready', function() {
var container = $('container');
var form = $('myForm');
var myFormValidator = new Form.Validator( form, {
stopOnFailure: true,
useTitles: true,
errorPrefix: "",
ignoreHidden : false,
onFormValidate: function( passed, form, event ) {
console.log( "Validation status: " + passed );
if ( passed ) {
event.stop();
// ...ajax request is here
}
}
});
new Element( 'a', {
'html': "To cart",
'class': 'shop_add',
'href': 'javascript:void(0)',
events: {
click: function(e) {
myFormValidator.validate(e);
}
}
}).inject( container );
});
用于测试的HTML
<div id="container"></div>
<form id="myForm" name="input" action="" method="get">
Username: <input type="text" name="user">
<input type="submit" value="Submit">
</form>