简体   繁体   English

Yii2按钮点击匿名功能

[英]Yii2 button onclick anonymous function

I am new to Yii2, and I am struggling to trigger an anonymous function by pressing a Yii2 button. 我是Yii2的新手,我正在努力通过按Yii2按钮来触发匿名功能。 Below are 6 samples, of which first two are OK. 下面是6个样本,其中前两个是好的。 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". 我想知道如何让一个匿名函数工作,比如“Button 3”和“Button 5”的情况。 I tested how to make a function call via Controller, and that works ok, but that is neither what I want. 我测试了如何通过Controller进行函数调用,这样可以正常工作,但这不是我想要的。 I would appreciate Your help - thanks! 非常感谢你的帮助 - 谢谢!

// 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();

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. 搜索Google以了解有关在Javascript中自动执行匿名函数的更多信息。 You should find tons of information and examples. 你应该找到大量的信息和例子。

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. 您可以尝试使用submitButton而不是按钮,如果您有其他submitButton插入条件,例如,当您使用第一个按钮时为0,当您使用第二个按钮时为1,然后将您的函数放入控制器并检查,第一次提交启动功能,第二次提交产生其他功能。 I know is not the best way, but it is the only way I imagine to do that. 我知道这不是最好的方式,但这是我想象的唯一方式。

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'
    ]
]);

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

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