lake
(Yuanhongxie)
January 23, 2009, 5:17am
1
I create a button in a form , I want to call a function in a js file.
how to do.
in user.php(view)
<?php
Yii::app()->clientScript->registerScriptFile(Yii::app()->basePath.'/js/test.js');
?>
…
…
<?php
echo CHtml::button('test script',array('click','test'=>'tested string'));
?>
test.js
function test(dd)
{
alert(dd);
}
Is it right if I write like this?
qiang
(Qiang Xue)
January 23, 2009, 12:02pm
2
Change 'basePath' to 'baseUrl'.
Change 'click' to 'onclick'.
lake
(Yuanhongxie)
January 26, 2009, 10:16pm
3
qiang ,it does not work. any other suggestion?
qiang
(Qiang Xue)
January 26, 2009, 10:21pm
4
What's the generated HTML?
lake
(Yuanhongxie)
January 26, 2009, 10:49pm
5
1.for the js file
<script type="text/javascript" src="/js/test.js"></script>
2.for the button
<input 0="onclick" test="tested string" name="yt1" type="button" value="test script"/>
qiang
(Qiang Xue)
January 27, 2009, 12:45am
6
The htmlOptions parameter to CHtml::button is incorrect. It should be an array of name-value pairs that you want to generate as attributes in the input tag.
lake
(Yuanhongxie)
January 28, 2009, 10:51pm
7
1.source code
<?php
echo CHtml::button('test script',array('click','test'=>'tested string'));
?>
2.the html result is :
<input 0="onclick" test="tested string" name="yt1" type="button" value="test script"/>
you mean that the result should be
<input 0=“onclick” test=>“tested string” name=“yt1” type=“button” value=“test script”/>
can u give me an example?
DarkNSF
January 28, 2009, 11:24pm
8
echo CHtml::button('test script',array('onclick'=>'myFunction(1337)'));
will produce
<input onclick="myFunction(1337)" name="yt0" type="button" value="test script"/>
they are HTML options, meaning they are just adding attributes to the input tag.