gpt4 book ai didi

jQuery 验证插件多种表单

转载 作者:行者123 更新时间:2023-12-01 04:11:52 25 4
gpt4 key购买 nike

我正在尝试向一系列表单添加一些功能以验证两个复选框。我正在使用 jQuery 验证插件,但我一生都无法让它工作。在 fiddle 中,当我提交时,我只是收到一条 403 禁止消息。在我的实际网站上,它看起来甚至从未执行过。

HTML - 这些表单是多个且每页相同的。不过 Stackoverflow 只让我展示一个

<form method="post" id="offer" name="offer" action="#" class="offer">
<input type="text" name="original_description">
<input type="text" name="description">
<input type="text" name="added_by">
<input type="submit" id="add" value="add" class="submit">
</form>

JS

$(".offer").each(function () {
$(this).validate({
submitHandler: function (form) {
var x = form.find('input[name=description]').val();
var y = form.find('input[name=original_description]').val();
var z = similar_text(x, y, 1);
alert("Description: " + x + " original_description: " + y + " z: " + z)
if (z > 50 && !isNaN(z)) {
alert("Description too similar to original. Please make it less similar");
return false;
}
form.submit();
},
rules: {
added_by: {
required: true
}
},
messages: {
added_by: {
required: 'Please select your name'
}
}
});
});

最佳答案

正如评论中所述,您应该在 rules: 之前添加一个“,”,并在您的submitHandler中操作jquery表单对象,如下所示:

$(".offer").each(function () {
$(this).validate({
submitHandler: function (form) {
var x = $(form).find('input[name=description]').val();
var y = $(form).find('input[name=original_description]').val();
var z = similar_text(x, y, 1);
alert("Description: " + x + " original_description: " + y + " z: " + z)
if (z > 50 && !isNaN(z)) {
alert("Description too similar to original. Please make it less similar");
return false;
}
form.submit();
},
rules: {
added_by: {
required: true
}
},
messages: {
added_by: {
required: 'Please select your name'
}
}
});
});

关于jQuery 验证插件多种表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19521696/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com