Javascript Yii2按钮onclick匿名函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27919491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:53:59  来源:igfitidea点击:

Yii2 button onclick anonymous function

javascriptfunctionbuttonyii2anonymous

提问by Kimmo from Jyvaskyla

I am new to Yii2, and I am struggling to trigger an anonymous function by pressing a Yii2 button. Below are 6 samples, of which first two are OK. But that is not exactly what I wish to have. I would like to know how I can get an anonymous function working, like cases for "Button 3" and "Button 5". I tested how to make a function call via Controller, and that works ok, but that is neither what I want. I would appreciate Your help - thanks!

我是 Yii2 的新手,我正在努力通过按下 Yii2 按钮来触发匿名功能。下面是 6 个样本,其中前两个是可以的。但这并不是我想要的。我想知道如何让匿名函数工作,例如“按钮 3”和“按钮 5”的情况。我测试了如何通过 Controller 进行函数调用,效果很好,但这不是我想要的。我会很感激你的帮助 - 谢谢!

// This works
$button1 = Button::begin ( 
[
'label' => 'Button 1',
'options' => [
    'class' => 'btn btn-primary',
    'onclick' => 'alert("Button 1 clicked");',
    ],
]);
$button1->run();

// This works
echo Html::button('Button 2', [ 'class' => 'btn btn-primary', 'onclick' => 'alert("Button 2 clicked");' ]);

// This DOES NOT work
echo Html::button('Button 3', [ 'class' => 'btn btn-primary', 'onclick' => 'function ( $event ) { alert("Button 3 clicked"); }' ]);

// This DOES NOT work
$button4 = Button::begin ( 
[
'label' => 'Button 4',
'options' => [
    'class' => 'btn btn-primary',
    // 'onclick' => 'alert("Button 1 clicked");',
    ],
]);
$button4->on('onclick', 'alert("Button 4 clicked");' );
$button4->run();


// This DOES NOT work
$button5 = Button::begin ( 
[
'label' => 'Button 5',
'options' => [
    'class' => 'btn btn-primary',
    'onclick' => 'function ( $event ) { alert("Button 5 clicked"); }',
    ],
]);
$button5->run();

// This DOES NOT work
$button6 = Button::begin ( 
[
'label' => 'Button 6',
'options' => [
    'class' => 'btn btn-primary',
    //'onclick' => 'function ( $event ) { alert("Button 4 clicked"); }',
    ],
]);
$button6->on('onclick', 'function ( $event ) { alert("Button 6 clicked"); }' );
$button6->run();

回答by Tahir

You can wrap your anonymous function in self-executing anonymous function ()().

您可以将匿名函数包装在自执行匿名函数中()()

So your second example would look something like this.

所以你的第二个例子看起来像这样。

echo Html::button('Button 3', [ 'class' => 'btn btn-primary', 'onclick' => '(function ( $event ) { alert("Button 3 clicked"); })();' ]);

Search Google to learn more about self-executing anonymous functions in Javascript. You should find tons of information and examples.

搜索 Google 以了解有关 Javascript 中自执行匿名函数的更多信息。您应该会找到大量信息和示例。

回答by r98inver

You can try something like using a submitButton instead of a button, if you have other submitButton insert a condition that is for example 0 when you use the first button and 1 when you use the second one, then put your function in the controller and check, the first submit launches the function, the second submit makes something else. I know is not the best way, but it is the only way I imagine to do that.

您可以尝试使用诸如使用 submitButton 而不是按钮之类的方法,如果您有其他 submitButton 插入一个条件,例如当您使用第一个按钮时为 0,使用第二个按钮时为 1,然后将您的功能放入控制器并检查,第一次提交启动函数,第二次提交做其他事情。我知道这不是最好的方法,但这是我想象的唯一方法。

Or, you can use ajaxSubmitButton:

或者,您可以使用 ajaxSubmitButton:

AjaxSubmitButton::begin([
    'label' => 'Check',
    'ajaxOptions' => [
        'type' => 'POST',
        'url' => 'country/getinfo',
        /*'cache' => false,*/
        'success' => new \yii\web\JsExpression('function(html){
            $("#output").html(html);
        }'),
    ],
    'options' => [
        'class' => 'customclass',
        'type' => 'submit'
    ]
]);