从Yii2中的View中的Javascript代码重定向到Controller Action

时间:2015-02-12 10:37:04

标签: yii2

$this->registerJS(
'var ok = confirm("File already exists!! Do you want to overwrite file");
    if(ok)
    {
       "'.Url::toRoute('default/over-write?id=1').'";
    }
    else
    {
       "'.Url::toRoute('default/over-write?id=0').'";
    };',View::POS_READY);

Url调用应该在DefaultController中调用actionOverWrite()。但这不会发生。如何从视图中调用操作?

2 个答案:

答案 0 :(得分:4)

您可以尝试:

$this->registerJS('
    var ok = confirm("File already exists!! Do you want to overwrite file");
    window.location.replace("'.Url::toRoute('default/over-write').'" + "?id=" + (ok ? 1 : 0));
', View::POS_READY);

阅读有关js重定向的更多信息: How to redirect to another webpage in JavaScript/jQuery?

答案 1 :(得分:1)

或试试这个

$this->registerJS(
'var url = "'.Url::toRoute('default/over-write?id=').'";
    if(confirm("File already exists!! Do you want to overwrite file"))
    {
       window.location.href = url + '1';
    }
    else
    {
       window.location.href = url + '0';
    };',View::POS_READY);