gpt4 book ai didi

javascript - Yii2 在另一个文件中调用 javascript 函数

转载 作者:行者123 更新时间:2023-12-03 03:56:07 25 4
gpt4 key购买 nike

我有一个 js 文件和一个索引文件,它们位于同一文件夹中。这是我的表格。我也已经注册了脚本。

$this->registerJs($this->render('script.js'), \yii\web\VIEW::POS_READY);

<?php $form = ActiveForm::begin(); ?>
<div class="booth">
<video id="video" width="400" height="300"></video>
<a href="#" id="capture" class="button">Take Photo</a>
<canvas id="canvas" width="400" height="300"></canvas>
</div>

<div class="form-group">
<?= Html::button('Save',['class'=>'btn btn-primary','onclick'=>'saveimage();']) ?>
</div>

<?php ActiveForm::end(); ?>

这是js文件中的函数。

function saveimage()
{
var csrfToken = $('meta[name="csrf-token"]').attr("content");

console.log(123);

})*/
}

当我单击按钮时显示错误未捕获的引用错误:saveimage 未定义?无论如何,它可以调用该函数吗?

我也尝试了 'onclick'=> 'js:saveimage()',但仍然无法正常工作。

最佳答案

尝试:

$this->registerJs(
"function saveimage()
{
var csrfToken = $('meta[name="csrf-token"]').attr("content");

console.log(123);

}",
\yii\web\VIEW::POS_READY,
'my-button-handler'
);

或者,如果您想使用单独的 js 文件,您可以使用以下语法:

$this->registerJsFile(
'@web/js/script.js',
['depends' => [\yii\web\JqueryAsset::className()]]
);

When working with the yii\web\View object you can dynamically register frontend scripts. There are two dedicated methods for this:

1) registerJs() for inline scripts

2) registerJsFile() for external scripts

更多详情请参阅link .

关于javascript - Yii2 在另一个文件中调用 javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44938897/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com