最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Yii2 button onclick anonymous function - Stack Overflow

programmeradmin8浏览0评论

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!

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

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!

// 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();
Share Improve this question edited Jan 13, 2015 at 12:50 Faenor 6331 gold badge9 silver badges24 bronze badges asked Jan 13, 2015 at 10:04 Kimmo from JyvaskylaKimmo from Jyvaskyla 711 gold badge1 silver badge2 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 16

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.

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.

Or, you can use 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'
    ]
]);
发布评论

评论列表(0)

  1. 暂无评论