JQuery 新手:从 JQuery 验证内部调用 Javascript 函数

发布于 2024-10-03 10:09:40 字数 824 浏览 0 评论 0原文

我的问题与此类似: 从 jquery success 调用 javascript 函数;但我没有使用 AJAX。

很简单,我正在验证一个表单(只是邮政编码)作为 Google 地图网络应用程序的一部分。验证代码是这样的:

$(document).ready(function($)
 {
  $("#clientInput").validate
   ({
    debug: true,

    rules:{zipcode: {required:true, minlength:5, maxlength:10, digits: true}},

    messages: {zipcode: {required: "You need to provide at least a zipcode.", minlength: "Please enter at least 5 digits", maxlength: "You've entered too many digits, please re-enter."}},


   });

   searchlocations();
});

粗体函数调用是对我的阻碍,它是对 Google Map API 函数的调用,但它永远不会被调用。 Google 地图 javascript 在此代码之前加载(尽管它位于单独的文件中),并且在添加验证代码之前,我通过“提交”按钮的 onclick() 事件调用了该函数。

如何在成功验证表单时调用此函数?

蒂亚!

My question is similar to this one: Call javascript function from jquery success; but I'm not using AJAX.

Very simply, I'm validating a form (just the zipcode) as part of a Google Maps web app. The validation code is as such:

$(document).ready(function($)
 {
  $("#clientInput").validate
   ({
    debug: true,

    rules:{zipcode: {required:true, minlength:5, maxlength:10, digits: true}},

    messages: {zipcode: {required: "You need to provide at least a zipcode.", minlength: "Please enter at least 5 digits", maxlength: "You've entered too many digits, please re-enter."}},


   });

   searchlocations();
});

The bold function call is what's stymieing me, it's a call to the Google Map API function but it never gets called. The Google Map javascript is loaded before this code (although it's in a separate file) and before I added the validation code I called the function through the Submit button's onclick() event.

How can I get this function called on successful validation of the form?

TIA!

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

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

发布评论

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

评论(2

濫情▎り 2024-10-10 10:09:40

您需要将该函数设置为用于验证的 submitHandler

You need to make the function the submitHandler for the validation.

不顾 2024-10-10 10:09:40

查克说得对;不过,这里有一个示例可以提供帮助:

$(document).ready(function($)
 {
  $("#clientInput").validate
   ({
    debug: true,

    rules:{zipcode: {required:true, minlength:5, maxlength:10, digits: true}},

    messages: {zipcode: {required: "You need to provide at least a zipcode.", minlength: "Please enter at least 5 digits", maxlength: "You've entered too many digits, please re-enter."}},

    submitHandler: function() {
            searchlocations();
    }

   });

});

如果您只需要调用 searchLocations,则可以通过直接分配 searchLocations 函数来使其变得更加简单:

    submitHandler: searchLocations

Chuck is right on; here's an example to help, though:

$(document).ready(function($)
 {
  $("#clientInput").validate
   ({
    debug: true,

    rules:{zipcode: {required:true, minlength:5, maxlength:10, digits: true}},

    messages: {zipcode: {required: "You need to provide at least a zipcode.", minlength: "Please enter at least 5 digits", maxlength: "You've entered too many digits, please re-enter."}},

    submitHandler: function() {
            searchlocations();
    }

   });

});

If you only need to call searchLocations, you can make it even simpler by just assigning the searchLocations function directly:

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