单击按钮时触发警报

发布于 2025-01-04 08:24:14 字数 1007 浏览 1 评论 0原文

当我单击该按钮时,不会执行警报。我做错了什么?也许我需要在标头部分包含一些 js 文件?

这是我的登录表单视图:

<?php echo CHtml::beginForm(); ?>

<div class="row">
<?php echo CHtml::label('username', 'username'); ?>
<?php echo CHtml::textField('username'); ?>
</div>
<div class="row">
<?php echo CHtml::label('password', 'password'); ?>
<?php echo CHtml::textField('password'); ?>
</div>
<?php
echo CHtml::ajaxButton('sign in', array('site/login'),array(
        'type'=>'POST',
        'update'=>'#mydiv',
        'beforeSend' => 'function(){
            alert("beforeSend");
        }',
        'complete' => 'function(){
            alert("complete");
            }',

));
?>
<?php echo CHtml::endForm(); ?>
<div id="mydiv" style="color:white;">...</div>

这是我在控制器中的代码:

public function actionLogin()
{
    $this->renderPartial('//blocks/user_info');
}

user_info 只是回显一些文本

When I click the button, alerts aren't performed. What am I doing wrong? Maybe I need to include some js files in my header section?

Here is my login form view:

<?php echo CHtml::beginForm(); ?>

<div class="row">
<?php echo CHtml::label('username', 'username'); ?>
<?php echo CHtml::textField('username'); ?>
</div>
<div class="row">
<?php echo CHtml::label('password', 'password'); ?>
<?php echo CHtml::textField('password'); ?>
</div>
<?php
echo CHtml::ajaxButton('sign in', array('site/login'),array(
        'type'=>'POST',
        'update'=>'#mydiv',
        'beforeSend' => 'function(){
            alert("beforeSend");
        }',
        'complete' => 'function(){
            alert("complete");
            }',

));
?>
<?php echo CHtml::endForm(); ?>
<div id="mydiv" style="color:white;">...</div>

Here is my code in the controller:

public function actionLogin()
{
    $this->renderPartial('//blocks/user_info');
}

user_info just echoes some text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

春风十里 2025-01-11 08:24:14

不确定这是否是您的问题,但 renderPartial() 不能很好地与 AJAX 配合使用。这是一个已知的 Yii 问题:

http://www.yiiframework。 com/forum/index.php/topic/24699-yii-20-ajaxrenderpartial-conflict/
http://www.yiiframework.com/forum/index.php?/topic/10427-ajax-clientscript

当我尝试通过 AJAX 加载表单,然后使用 CHtml::ajaxSubmitButton 将其提交回来时,我遇到了问题。

Not sure if this is your problem, but renderPartial() does not play well with AJAX. It is a known Yii issue:

http://www.yiiframework.com/forum/index.php/topic/24699-yii-20-ajaxrenderpartial-conflict/
http://www.yiiframework.com/forum/index.php?/topic/10427-ajax-clientscript

I ran into the problem when I tried to load a form via AJAX, then submit it back using CHtml::ajaxSubmitButton.

送君千里 2025-01-11 08:24:14

试试这个

<代码>
this->renderPartial('//blocks/user_info','', false, true);

try this


this->renderPartial('//blocks/user_info','', false, true);

百变从容 2025-01-11 08:24:14

在我的代码中它可以尝试这个

<?php
echo CHtml::ajaxSubmitButton('Save','your-url',array(
   'type'=>'POST',
   'dataType'=>'json',
   'success'=>'js:function(data){
       if(data.result==="success"){
          // do something on success, like redirect
       }else{
         $("#some-container").html(data.msg);
       }
   }',
));
?>

in my code it works try this

<?php
echo CHtml::ajaxSubmitButton('Save','your-url',array(
   'type'=>'POST',
   'dataType'=>'json',
   'success'=>'js:function(data){
       if(data.result==="success"){
          // do something on success, like redirect
       }else{
         $("#some-container").html(data.msg);
       }
   }',
));
?>
左耳近心 2025-01-11 08:24:14

beforeSend 有效吗?如果你尝试“成功”而不是“完成”怎么办?

Does beforeSend work? What if you try "success" instead of "complete"?

淤浪 2025-01-11 08:24:14

我也有这个问题(bug?)。
尝试在此视图中更改“beforeSend”和其他回调:

'beforeSend':'js:alert("complete");'

不使用“function(){}”并使用“js”。

聚苯乙烯
抱歉,我的英语不好(

I have this problem(bug?) too.
Try change 'beforeSend' and other callbacks in this view:

'beforeSend':'js:alert("complete");'

without "function(){}" and with "js".

P.S.
Sorry, my English is bad(

冷心人i 2025-01-11 08:24:14

这是 Yii Ajax 提交按钮的简单示例:

<?php
echo CHtml::ajaxSubmitButton(
'Submit',
'your-url',
array(
   'type'=>'POST',
   'dataType'=>'json',
   'success'=>'js:function(data){
       if(data.result==="success"){
          // do something on success, like redirect
       }else{
         $("#some-container").html(data.msg);
       }
   }',
)
);
?>

更多详细信息请访问 http://www.codexamples.com /90/chtml-ajaxsubmitbutton/

This is simple example of Yii Ajax Submit button:

<?php
echo CHtml::ajaxSubmitButton(
'Submit',
'your-url',
array(
   'type'=>'POST',
   'dataType'=>'json',
   'success'=>'js:function(data){
       if(data.result==="success"){
          // do something on success, like redirect
       }else{
         $("#some-container").html(data.msg);
       }
   }',
)
);
?>

More detail at http://www.codexamples.com/90/chtml-ajaxsubmitbutton/

顾北清歌寒 2025-01-11 08:24:14

视图中的 Ajax 提交按钮:

echo CHtml::ajaxSubmitButton('Save', array('/controller/action/id/' .     $id), array(
                'type' => 'post',
                'dataType' => 'json',
                'update' => '#mydiv',
                'beforeSend' => 'function(){}',
                'complete' => 'function(){}',
                'success' => 'js:function(data){
                    if(data.status=="1"){
                        html = "<div class=\"alert in alert-block fade alert-success\"><a class=\"close\" data-dismiss=\"alert\">x</a>"+data.msg+"</div>";
                        $("#status-msg").html(html);

                        }else{
                        html = "<div class=\"alert in alert-block fade alert-error\"><a class=\"close\" data-dismiss=\"alert\">x</a>"+data.msg+"</div>";
                        $("#status-msg").html(html);
                        }
                    }',
                    ), array('class' => "btn btn-primary")
            );

在控制器操作中:

        $return = array();
        $return['status'] = '1';
        $return['msg'] = 'Record Updated Successfully';

        echo CJSON::encode($return);
        Yii::app()->end();

根据要求更改返回数组。

Ajax submit button in view:

echo CHtml::ajaxSubmitButton('Save', array('/controller/action/id/' .     $id), array(
                'type' => 'post',
                'dataType' => 'json',
                'update' => '#mydiv',
                'beforeSend' => 'function(){}',
                'complete' => 'function(){}',
                'success' => 'js:function(data){
                    if(data.status=="1"){
                        html = "<div class=\"alert in alert-block fade alert-success\"><a class=\"close\" data-dismiss=\"alert\">x</a>"+data.msg+"</div>";
                        $("#status-msg").html(html);

                        }else{
                        html = "<div class=\"alert in alert-block fade alert-error\"><a class=\"close\" data-dismiss=\"alert\">x</a>"+data.msg+"</div>";
                        $("#status-msg").html(html);
                        }
                    }',
                    ), array('class' => "btn btn-primary")
            );

In Controller action:

        $return = array();
        $return['status'] = '1';
        $return['msg'] = 'Record Updated Successfully';

        echo CJSON::encode($return);
        Yii::app()->end();

Change return array as per requirement.

浮世清欢 2025-01-11 08:24:14

您必须将 renderPartial 函数的第四个参数设置为 true,以便 Yii 包含视图正常工作所需的 JS 文件。

$this->renderPartial('_form',array('model'=>$model), false, true);

You have to set the fourth parameter of renderPartial function to true, so that Yii will include the required JS files for the proper working of the view.

$this->renderPartial('_form',array('model'=>$model), false, true);
玻璃人 2025-01-11 08:24:14

在控制器函数中,您需要再发送 3 个参数(总共 4 个参数),

您的函数将如下所示

public function actionLogin()
{
  this->renderPartial('//blocks/user_info','',false,true);
}

In the controller function you need to send 3 more parameters (total 4 parameters)

your function will be like this

public function actionLogin()
{
  this->renderPartial('//blocks/user_info','',false,true);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文