简体   繁体   English

将变量传递给JavaScript函数

[英]Passing a variable into a JavaScript Function

I have written a Javascript Function 我写了一个Javascript函数

jQuery(document).ready( function newbie($) {

    //var email = 'emailaddress'
    var data = {
        action: 'test_response',
        post_var: email
    };
    // the_ajax_script.ajaxurl is a variable that will contain the url to the ajax processing file
    $.post(the_ajax_script.ajaxurl, data, function(response) {
        alert(response);
    });
    return false;
});

Which I will call using 我会用这个称呼

newbie();

But I want to pass in a variable (the email address) when I call the function but I am not sure how to do this. 但是我想在调用函数时传递一个变量(电子邮件地址),但是我不确定该怎么做。 That $ sign seems to get in my way! 这个美元符号似乎妨碍了我! Any thoughts much appreciated. 任何想法表示赞赏。

jQuery(document).ready(function(){
   var email = 'emailaddress';
   newbie(email);
});


function newbie(email) {
    var data = {
        action: 'test_response',
        post_var: email
    };
    // the_ajax_script.ajaxurl is a variable that will contain the url to the ajax processing file
    $.post(the_ajax_script.ajaxurl, data, function(response) {
        alert(response);
    });
    return false;
}

OR 要么

 jQuery(document).ready(function(){
        var newbie = function(email) {
           var data = {
               action: 'test_response',
               post_var: email
           };
           // the_ajax_script.ajaxurl is a variable that will contain the url to the ajax processing file
           $.post(the_ajax_script.ajaxurl, data, function(response) {
               alert(response);
           });
           return false;
      }

     var email = 'emailaddress';
     newbie(email);
  });

Functions in javascript take 'arguments'. javascript中的函数带有“参数”。 You can pass in as many arguments you want and define their name space in the function declaration. 您可以传入任意多个参数,并在函数声明中定义其名称空间。 ie

function foo(bar,baz,etc){
   console.log(bar,baz,etc);
}
foo(1,2,3)
 //logs out 1 2 3

Sometimes you don't always know what's going to be passed in or how many arguments there are going to be, in this case inside of the function declaration we can use the 'arguments' object to pick out certain arguments passed into the function. 有时您并不总是知道要传递什么或要传递多少个参数,在这种情况下,在函数声明内部,我们可以使用“ arguments”对象来选择传递给函数的某些参数。

function foo(){
   console.log(arguments);
}
foo(1,2,3)
 //logs out an array object that looks like this [1,2,3]

jQuery(document).ready( function newbie($, email) { jQuery(document).ready(function newbie($,email){

//var email = 'emailaddress'
var data = {
    action: 'test_response',
    post_var: email
};
// the_ajax_script.ajaxurl is a variable that will contain the url to the ajax processing file
$.post(the_ajax_script.ajaxurl, data, function(response) {
    alert(response);
});
return false;

}); });

you simply call the function by passing the values 您只需通过传递值来调用函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM