在jScript中合并两个函数 [英] Merge two functions in jScript

查看:103
本文介绍了在jScript中合并两个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的页面上使用AJAX调用PHP脚本(表单验证)。我对PHP非常方便,但对javascrip,jScript或任何其他客户端语言不太好,所以我找到了一个免费的演示脚本来满足我的需求,现在我只是为了看看我是否可以学习在这个过程中。有一件事我很好奇,如果我可以将两个函数合并为一个。



函数看起来基本如下:

  //当用户改变形式
$(#signup)。change(function()
{
//。 ..验证代码在这里

$ b $ //当form(page)首次加载
$(#signup)。ready(function()
{
// ...验证代码
}

第一个函数验证如果事情发生了变化,

有时用户会过早地提交表单(例如按回车键),在这种情况下,验证不会再次发生,直到用户输入改变表单中的内容。

我使用验证结果来决定应用于输入元素的类(告诉用户做了什么而不是),我想如果表单没有完整地提交,那么这将留下(或立即再次发生)。我知道我可以申请表单验证不会让用户提交,直到填写所有必填字段,但让我们假装这是不可能的一些未知的原因...



第二个函数只是第一个副本一个页面加载后立即验证表单。这个最初并不在演示脚本中,但我发现我可以复制第一个函数,并将change改为ready,以便我的表单得到验证。



现在,出于好奇,有没有办法将这些合并成一个函数,而不是两个完全相同?像PHP中的OR运算符:

  $(#signup)。change(function()|| $( #signup)。ready(function()
{
// ...验证码在这里
}

编辑:是的,我用google搜索了一下,虽然没有成功,也许是由于我缺乏javascript / jScript知识,我没有使用正确的搜索条件...

b $ b

解决方案

没有你描述的东西这样的事情,但是这给了你相同的效果。

  var validate = function(){
// ...此处验证代码
}

$(#signup ).change(validate);
$(#signup)。ready(validate);


$ b $你可以创建一个函数,赋值给一个变量,然后对该函数进行引用,然后该引用可以传递给需要函数作为参数的任何东西 - .change,.ready,setTimeout,setInterval,等等。

I need to call a PHP script using AJAX on my page (form validation). I'm quite handy with PHP but not very good with javascrip, jScript or any other client side language so I've found a free demo script that fills my needs and now I'm playing around with it just to see if I can learn something in the process. One thing I'm curious about if I can merge two functions into one.

The functions looks basically like this:

// When user changes something in the form
$("#signup").change(function()
{
    // ... validation code here
}

// When the form (page) first loads
$("#signup").ready(function()
{
    // ... validation code here
}

The first function validates the form if something is changed.

Sometimes users will submit the form prematurely (by pressing enter, for example) and in that case the validation will not occur again until user input changes something in the form.

I use the validation result to decide what class to apply to the input elements (to tell the user what is done and not) and I want this to stay (or happened again immediately ) if the form is submitted without being complete. I know I can apply form validation that wont let user submit until all required fields are filled out but let's pretend this is not possible for some unknown reason...

The second function is just a copy of the first one and validates the form as soon as the page loads. This one was not originally in the demo script but I figured out that I could duplicate the first function and change the word "change" to "ready" to get my form validated as I wanted.

Now, just out of curiosity, is there a way to merge those into one function instead of having two identical? Something like the OR operator in PHP:

$("#signup").change(function() || $("#signup").ready(function()
{
    // ... validation code here
}

Edit: yes, I have googled about it. Unsuccessfully though, maybe due to my lacking javascript/jScript knowledge I'm not using the correct search terms...

解决方案

There's no such thing as what you're describing, but this gives you the same effect.

var validate = function () {
    // ... validation code here
}

$("#signup").change(validate);
$("#signup").ready(validate);

You can create a function, assign to to a variable and then have a reference to that function. That reference can then be passed to anything that requires a function as a parameter - .change, .ready, setTimeout, setInterval, etc., etc.

这篇关于在jScript中合并两个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆