如何将 jQuery 验证插件与文本区域一起使用?
我在理解如何实现 jQuery 验证插件 时遇到了一些困难。对于使用 Ajax 提交的表单:
$(document).delegate("'#submit-quote'", "submit", function(){
var quoteVal = $(this).find('[name="quote"]').val();
$.post("add.php", $(this).serialize(), function(data) {
var like = $('.quote-wrap span iframe');
$('.inner').prepend('<div id="' + data + ' " class="quote-wrap group">' + like + '<div class="quote"><p>' + quoteVal+ '</p></div></div>');
// console.log("success");
var id = parseInt(data);
console.log(id)
// some code after being successfully sent
});
添加验证插件后,我将如何设置默认值并确保这是我想要表单执行的操作?
它只是一个文本区域,用户可以提交简单的报价。 (文本和数字)并且不应提交任何其他内容。现在我可以提交 HTML 或我喜欢的任何其他内容。
我需要做什么来确保用户不能?
我尝试过:
I am having a little bit of trouble understanding how to implement jQuery Validation Plugin. With my form that is being submitted with Ajax:
$(document).delegate("'#submit-quote'", "submit", function(){
var quoteVal = $(this).find('[name="quote"]').val();
$.post("add.php", $(this).serialize(), function(data) {
var like = $('.quote-wrap span iframe');
$('.inner').prepend('<div id="' + data + ' " class="quote-wrap group">' + like + '<div class="quote"><p>' + quoteVal+ '</p></div></div>');
// console.log("success");
var id = parseInt(data);
console.log(id)
// some code after being successfully sent
});
After adding validation plugin, how would I set the defaults and make sure it's what I want the form to do?
It's just a textarea, that a user submits a simple quote. (text and numbers) and shouldn't submit anything else. Right now I can submit HTML, or anything else I like.
What do I need to do to make sure the user can't?
I have tried:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您需要做的只是确保用户无法发布 HTML,请查看 StackOverflow 上的这篇文章:从输入字段读取属性时 HTML 编码丢失。
否则,您可以使用 jQuery Validation 在验证过程中执行类似的方法。
If all you need to do is make sure users can't post HTML, then check out this article on StackOverflow: HTML-encoding lost when attribute read from input field.
Otherwise, you can use jQuery Validation to perform a similar method during validation.
试试这个
你可以检查表格的有效性。如果有效,请做好您的工作。否则返回 false,
检查此
http://docs.jquery.com/Plugins/Validation/valid< /a>
还有一件事,您正在使用表单的
submit
事件。导致页面刷新。所以你必须返回 false 或使用 http://api.jquery.com/event.preventDefault/< /a>
try this
you can check the validity of the form . if valid do your work . else return false,
check this
http://docs.jquery.com/Plugins/Validation/valid
and one more thing, you are using
submit
event of the form. that causes page refresh. so you have toreturn false OR use http://api.jquery.com/event.preventDefault/