简体   繁体   English

jQuery,将click事件附加到函数并与一串javascript调用结合?

[英]jquery, attaching click event to a function and combining with a string of javascript calls?

I'm trying to attach a click event to a button that runs calls from both a parameter and from itself. 我试图将click事件附加到一个按钮,该按钮可同时运行参数和其自身的调用。

What I want is a 1st alert to always show, then execute the 2nd alert that is stored in myparam.onclick as a string. 我想要的是始终显示第一警报,然后执行存储在myparam.onclick中的第二警报(作为字符串)。

Non-working attempt: 非工作尝试:

var myparam = { onclick: 'alert("second");' };
$('#my_button_id').on('click', function(){ alert('first'); myparam.onclick }); 

I know that if I change myparam to a legitimate function, such as: 我知道如果将myparam更改为合法函数,例如:

var myparam = {onclick: function(){alert('second');} }

I can call that just fine by 我可以这样称呼

$('#my_button_id').on('click', myparam.onclick);

but I still don't know how to concatenate two together. 但我仍然不知道如何将两个连接在一起。 I don't want to call the first alert every time in the myparam func call. 我不想每次在myparam func调用中都调用第一个警报。

Any help is appreciated. 任何帮助表示赞赏。

You can use the eval function to execute javascript code as String. 您可以使用eval函数将JavaScript代码作为String执行。

 var myparam = { onclick: 'alert("second");' }; $('#my_button_id').on('click', function() { alert('first'); eval(myparam.onclick); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script> <button id="my_button_id">Click</button> 

You should see that mate: https://stackoverflow.com/a/12651991/6158032 您应该看到那个伴侣: https : //stackoverflow.com/a/12651991/6158032

My little help is.. 我的小帮助是..

//Clousure 
(function($) {
    //Document.ready
    $(function(){
    //I think you want this ?
    let myparam = { onclick: function() { alert("second"); } };
    $('#my_button_id').on('click', function(){ alert('first'); myparam.onclick() }); 
    //Another way.
    function doAlert (messages) {
        alert(messages);
    }
    //Params Object
    let my_params = {
        onclick_first : function () { return doAlert('first') },
      onclick_second : function () { return doAlert('second') }
    }
    //Event Click
    $('#my_button_id').on('click',function(){
        //First Alert
        my_params.onclick_first();
      //Second Alert
      my_params.onclick_second();
    });
  });
})(jQuery);

JSFIDDLE DEMO JSFIDDLE演示

It was close: 很接近:

var myparam = {onclick: function(){alert('second');} };
$('#my_button_id').on('click', function(){ alert('first'); myparam.onclick() }); 

You missed a parenthesis at the end f the function call: myparam.onclick() 您在函数调用的末尾错过了括号: myparam.onclick()

If you really needed a string you can create a function and supply the body as a string then call it: 如果您确实需要一个字符串,则可以创建一个函数并将主体作为字符串提供,然后调用它:

 var myparam = { onclick: 'alert("second");' }; $('#my_button_id').on('click', function(){ alert('first'); var fun = Function(myparam.onclick); fun(); }); 

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

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