Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/366.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_Jquery Selectors - Fatal编程技术网

Javascript 如何使用作为函数参数传递的变量 使现代化

Javascript 如何使用作为函数参数传递的变量 使现代化,javascript,jquery,jquery-selectors,Javascript,Jquery,Jquery Selectors,我有一个小的打字错误,一切都正常 我不知道如何在多个ajax调用中使用作为函数参数传递的变量作为选择器 第一个和第二个都可以,但在调用第三个调用函数时,单击“#按钮-”+id_编号”不会触发任何事件,尽管警报(“按钮-”+id_编号)显示了正确的元素“按钮-0”#按钮-1…等等 $(document).ready(function() { // FIRST $("#process").click(function(){ var somedata = $("

我有一个小的打字错误,一切都正常

我不知道如何在多个ajax调用中使用作为函数参数传递的变量作为选择器

第一个和第二个都可以,但在调用第三个调用函数时,单击“#按钮-”+id_编号”不会触发任何事件,尽管警报(“按钮-”+id_编号)显示了正确的元素“按钮-0”#按钮-1…等等

$(document).ready(function() {
        // FIRST
    $("#process").click(function(){
        var somedata = $("#somedata").val();
          $('input[type="text"]').removeClass("error");
          $.ajax({
              url: 'first.php',
              type: "POST",
              data: ({somedata: somedata }),
              beforeSend:function(){
                jQuery('#ajax-panel').html('<div class="loading"><img src="loading.gif" alt="Loading..." /></div>');
              },
              success: function(data){
                  $("#ajax-panel").html(data);
                      SecondCallbackFunction();
              },
              error:function(){
                jQuery('#ajax-panel').html('<p class="error">Oops!</p>');
              }
          }); 
        });
});
        // SECOND
        function SecondCallbackFunction(){
            $(".load-button").click(function(){
                    var id = $(this).closest('.box').attr('id');
                    var arr = id.split('-');
                    var id_number = arr[1];
                    alert(id_number);  //
                    $.ajax({
                        url: 'second.php',
                        type: "POST",
                        data: ({id_number: id_number }),
                        beforeSend:function(){
                          $("#"+id).find('.load-button').hide();    
                          $("#"+id).append('<div class="loading"><img src="loading.gif" alt="Loading..." /></div>');
                        },
                        success: function(data){
                            $("#"+id).find('.loading').hide();  
                            $("#"+id).append(data);
                            ThirdCallbackFunction(id_number);
                        },
                        error:function(){
                          $("#"+id).append('<p class="error">Oops!</p>');
                        }
                    });

            });
        }
        // THIRD
        function ThirdCallbackFunction(id_number){
            alert(id_number);  // SHOWING 1 , 2 ....etc
            $("#button-" + id_number).bind("click", function() {   // DOING nothing
                    var title = $('#title' + id_number).val();              
                    $.ajax({
                        url: 'third.php',
                        type: "POST",
                        data: ({title: title, 
                        }),
                        beforeSend:function(){
                           $("#item"+id_number).append('<div class="loading"><img src="loading.gif" alt="Loading..." /></div>');
                        },
                        success: function(data){
                             $("#item"+id_number).find('.loading').hide();  
                             $("#item"+id_number).append(data);
                        },
                        error:function(){
                          $("#"+id).append('<p class="error">Oops!</p>');
                        }
                    });
                });
        }
$(文档).ready(函数(){
//首先
$(“#进程”)。单击(函数(){
var somedata=$(“#somedata”).val();
$('input[type=“text”]”)。removeClass(“错误”);
$.ajax({
url:'first.php',
类型:“POST”,
数据:({somedata:somedata}),
beforeSend:function(){
jQuery('#ajax panel').html('';
},
成功:功能(数据){
$(“#ajax面板”).html(数据);
SecondCallbackFunction();
},
错误:函数(){
jQuery(“#ajax panel”).html(“

Oops!

”); } }); }); }); //第二 函数SecondCallbackFunction(){ $(“.load按钮”)。单击(函数(){ var id=$(this).closest('.box').attr('id'); var arr=id.split('-'); var id_编号=arr[1]; 警报(id_编号)// $.ajax({ url:'second.php', 类型:“POST”, 数据:({id\u number:id\u number}), beforeSend:function(){ $(“#”+id).find('.load按钮').hide(); $(“#”+id).append(“”); }, 成功:功能(数据){ $(“#”+id).find('.loading').hide(); $(“#”+id).append(数据); 第三个调用函数(id_编号); }, 错误:函数(){ $(“#”+id).append(“

Oops!

”); } }); }); } //第三 函数ThirdCallbackFunction(id\U编号){ 警报(id_编号);//显示1、2…等 $(“#按钮-”+id_编号)。绑定(“单击”,函数(){// var title=$('#title'+id_number).val(); $.ajax({ url:'third.php', 类型:“POST”, 数据:({标题:标题, }), beforeSend:function(){ $(“#项目”+id_编号)。附加(“”); }, 成功:功能(数据){ $(“#项”+id_编号)。查找('.loading')。隐藏(); $(“#项”+id_编号)。附加(数据); }, 错误:函数(){ $(“#”+id).append(“

Oops!

”); } }); }); }
我不太理解您的问题,但是如果您想使用任何变量、对象或任何可以定义为参数的东西,只需将其包装为数组
[Variable]
(如果还没有),获取此变量将作为参数传递给的函数,然后使用apply函数<代码>函数应用(此[变量])如果您碰巧知道要传递的变量,您只需正常传递即可:

(function() { })(variable1, variable2, /* ect... */);
或使用调用功能:

func.call(this, variable1, variable2);

其中
func
是您正在调用的函数。您还可以将关键字
this
替换为任何内容。如果执行此操作,则函数中对该的任何引用都将引用您提供的值。这就是像jQuery这样的框架如何允许您通过引用其框架内的
This
来访问jQuery对象

显然你的代码没有错。请发布实际重现问题所需的HTML和Javascript,并仅保留相关函数。控制台中有任何内容吗?在
ThirdCallbackFunction
中执行所需选择器的
console.log
时,是否显示有匹配的DOM元素?在第三个单击处理程序中是否显示了
console.log
?我有一个小的输入错误,它的一切都正常工作。