Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/421.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将变量传递到JavaScript函数_Javascript_Jquery - Fatal编程技术网

将变量传递到JavaScript函数

将变量传递到JavaScript函数,javascript,jquery,Javascript,Jquery,我已经编写了一个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 ur

我已经编写了一个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;
});
我会称之为使用

newbie();

但我想在调用函数时传入一个变量电子邮件地址,但我不确定如何执行此操作。那块$sign似乎挡住了我的路!非常感谢您的任何想法。

javascript中的函数采用“参数”。您可以传入任意数量的参数,并在函数声明中定义它们的名称空间。即

function foo(bar,baz,etc){
   console.log(bar,baz,etc);
}
foo(1,2,3)
 //logs out 1 2 3
有时您并不总是知道将要传入什么或将有多少个参数,在本例中,在函数声明中,我们可以使用“arguments”对象来选择传递到函数中的某些参数

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


jQuerydocument.ready函数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;
})


您只需通过传递值来调用函数

,这是一个命名函数表达式,它的名称只有它自己知道,感谢Mohamed。如果使用第一个示例,将代码的第一位放在.js函数文件中,第二位放在页面中,则什么都不会发生。关于我做错了什么有什么想法吗?@LinzDarlington密切关注控制台是否有错误..出现了什么错误???@LinzDarlington文件的排列。*在第一个示例中*。1-包含jquery 2-在document ready上运行函数3-包含函数js文件,您也可以在运行函数3然后2而不是2然后3之前包含函数js文件。。但是在我的第二个示例1(包括jquery 2)中,请在运行函数之前检查是否定义了var newbie。。有了这样的安排,一切都会好起来的谢谢穆罕默德。将检查并恢复。
 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);
  });
//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;