如何将 jQuery 验证插件与文本区域一起使用?

发布于 2024-12-27 01:51:09 字数 887 浏览 3 评论 0原文

我在理解如何实现 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑叹一世浮沉 2025-01-03 01:51:10

如果您需要做的只是确保用户无法发布 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.

柒七 2025-01-03 01:51:10

试试这个

$("#myform").validate();
$("a.check").click(function () {
    if ($("#myform").valid()) {
        //if valid do form submit
    }
    else {
        return false;
    }
});

你可以检查表格的有效性。如果有效,请做好您的工作。否则返回 false,

检查此

http://docs.jquery.com/Plugins/Validation/valid< /a>

还有一件事,您正在使用表单的 submit 事件。导致页面刷新。所以你必须

返回 false 或使用 http://api.jquery.com/event.preventDefault/< /a>

try this

$("#myform").validate();
$("a.check").click(function () {
    if ($("#myform").valid()) {
        //if valid do form submit
    }
    else {
        return false;
    }
});

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 to

return false OR use http://api.jquery.com/event.preventDefault/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文