如何在 Yii 页面上使用带有 CHtml button() 的 JavaScript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15149796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 23:42:15 来源:igfitidea点击:
How to use JavaScript with CHtml button() on a Yii page
提问by sher
I'm trying to use JavaScript in my page. My JavaScript code is:
我正在尝试在我的页面中使用 JavaScript。我的 JavaScript 代码是:
function edit(){
alert("test");
return document.getElementById("confirm").value="";
}
Calling this, by onclick
function
通过onclick
函数调用这个
<?php echo CHtml::button("Edit",array('title'=>"Edit",'onclick'=>'js:edit();')); ?>
both where in same page and confirmation.php
都在同一页面和confirmation.php
回答by Odiljon Sattarov
Yii::app()->clientScript->registerScript('register_script_name', "
$('#editButton').click(function(){
alert('edit');
return false;
});
", CClientScript::POS_READY);
<?php echo CHtml::button("Edit",array('title'=>"Edit",'id'=>"editButton")); ?>
回答by Samuel Liew
Set an ID for your button:
为你的按钮设置一个 ID:
<?php echo CHtml::button("Edit",array('title'=>"Edit",'id'=>'myButton')); ?>
JS:
JS:
window.onload = function() {
document.getElementById("myButton").onclick = function() {
alert("test");
document.getElementById("confirm").value = "";
return false;
}
}
Or you can just remove the js:
part from your code:
或者您可以js:
从代码中删除该部分:
<?php echo CHtml::button("Edit",array('title'=>"Edit",'onclick'=>'edit()')); ?>