如何在 ajax 请求返回的渲染表单上使用 jquery.validete

发布于 2024-12-26 12:04:22 字数 361 浏览 3 评论 0原文

我在使用 jquery 验证插件时遇到一个小问题。

它与正常页面加载期间呈现的静态表单完美配合。 我用于验证的代码看起来像这样

$('#form_name).validate({ (...) and other stuff

问题是当我使用 ajax 请求创建具有相同 ID 的弹出表单时,它将无法工作。

我找不到这个问题的正确答案,所以我来到这里。我想 $().live 方法可能很有用,但我不知道如何在没有事件(如“点击”、“提交”等)时使用它。

哦,并防止页面出现其他问题加载新的 ajax 表单的位置与呈现静态表单的页面不同,因此同一页面上的两个相同表单不会发生冲突。

I have a slight problem with usage of jquery validation plugin.

It works perfectly with static forms rendered during normal pageload.
The code I use for validation looks something likie this

$('#form_name).validate({ (...) and other stuff

The problem is when I use ajax request to create popup form with the same ID it won't work.

I couldn't find proper answer form this issue so I came here. I guess the $().live method could be usefull but I have no idea how to use it on no event like "click", "submit" etc..

Ow and to prevent other questions the page where new ajax form is loaded is located somwhere else than the page where the static form is rendered so there is no conflicts of two identical forms on the same page.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

泡沫很甜 2025-01-02 12:04:22

我以前遇到过这个问题。新添加的 ajax 内容可能需要重新初始化触发器。您可以采取多种方法。以下只是其中之一。

将 javasciprt 绑定触发器放在一个函数中,并在 ajax 请求后调用它。

<html>
<script type="text/javascript">

 // when page has loaded do the binds 
 $(document).ready(function(){
   initBinds();
 });

 // This is your ajax request psuedo code
 function someAjaxRequest(){

    // 1) do some ajax request then 
    // 2) load the data 
    // 3) after this has been done then call initBinds() 
          function to re-init new data with bind triggers
 }

function initBinds()    {
   $('#someID').bind('click', function(){
     // blah blah
   })


   $('#someOtherID').bind('click', function(){
     // blah blah
   })

}
</spript>

</html>

I have come across this problem before. Newly added ajax content may need the triggers reinitialising. There are several approaches you can take. Below is just one of them.

put the javasciprt bind triggers inside a function and call it after the ajax request.

<html>
<script type="text/javascript">

 // when page has loaded do the binds 
 $(document).ready(function(){
   initBinds();
 });

 // This is your ajax request psuedo code
 function someAjaxRequest(){

    // 1) do some ajax request then 
    // 2) load the data 
    // 3) after this has been done then call initBinds() 
          function to re-init new data with bind triggers
 }

function initBinds()    {
   $('#someID').bind('click', function(){
     // blah blah
   })


   $('#someOtherID').bind('click', function(){
     // blah blah
   })

}
</spript>

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