【问题标题】:How to pass argument to a jquery function through an onClick event? [duplicate]如何通过 onClick 事件将参数传递给 jquery 函数? [复制]
【发布时间】:2012-09-04 17:07:59
【问题描述】:

可能重复:
jquery .click pass parameters to user function

    $('#topleft').click(function (x) {
    loadPopupBox();
    $("#popupinner").load('getdetail.php?id=' +x);
    });

如何将值从 onclick 事件传递给变量 x?变量 x 的值被 POSTED 到 getdetail.php 并且从 getdetail.php 返回的数据被加载到 ID 为 popupinner 的 DIV。现在一切正常,除了数据 POST/GET。

【问题讨论】:

    标签: jquery


    【解决方案1】:

    尝试将.click 语法用作.click( [eventData], handler(eventObject) )

    见下文,

    //                    v- Pass data
    $('#topleft').click({x:x}, function (event) {
        loadPopupBox();
        $("#popupinner").load('getdetail.php?id=' + event.data.x); //read data from event obj
    });
    

    [eventData] x 可以作为event.data.x 访问

    【讨论】:

      【解决方案2】:

      您可以将 eventData 提供给click 函数:

      $('#topleft').click({x:x}, function (e) {
          loadPopupBox();
          $("#popupinner").load('getdetail.php?id=' +e.datax);
      });
      

      【讨论】:

        【解决方案3】:

        您需要的是click event handler。 请参阅此question 以了解其实际工作原理。

        【讨论】:

        • 这应该是一条评论,而不是“可能重复...”或类似的内容
        【解决方案4】:
        // say your selector and click handler looks something like this...
        $("some selector").click({param1: "Hello", param2: "World"}, cool_function);
        
        // in your function, just grab the event object and go crazy...
        function cool_function(event){
            alert(event.data.param1);
            alert(event.data.param2);
        }
        

        jQuery's .click - pass parameters to user function中所见

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-02-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-04-07
          • 1970-01-01
          • 1970-01-01
          • 2020-04-18
          相关资源
          最近更新 更多