I have a table student
id
roll_no
Sub1_mark
sub2_mark
total
percentage
i want to calculate total mark on the lost focus of sub2_mark (when i enter sub1_mark & sub2_mark total display in total text field)
please help mi i cant know how to write & call javascript in yii.
oligalma
(Marc Oliveras)
December 18, 2014, 12:54pm
2
you should use the registerscript or registerscriptfile
$script = '
$(document).ready(function(){
alert("hello");
});
';
$baseUrl = Yii::app()->baseUrl;
$cs = Yii::app()->getClientScript();
$cs->registerScriptFile($baseUrl.'/js/myscript.js',CClientScript::POS_END);
$cs->registerScript('myscript', $script);
If you want to register css use registercss instead
SiZE
(Sizemail)
December 18, 2014, 12:54pm
3
Your view:
<?php
Yii::app()->clientScript->registerScript('calc_total_mark','
// your js code here
');
?>
<!-- some html code -->
thxy for replay i will try
This seems like a simple js task, don’t think that is necessary to do it on separate file, U can do like on any file, place your js code inside <script></script>.
i wrote this java script in _form where i call this i have no error but it cant displt value in total field.
Yii::app()->clientScript->registerScript(‘totalAmount’,’
$(document).ready(function()
{
$("#C , #Cpp ").onchange(function(event){
var $subtotal = $("#C").val() + $("#Cpp").val();
$("#Total").val($subtotal);
});
');
SiZE
(Sizemail)
December 19, 2014, 6:46am
7
It’s a good practice to move <script> before </html>, so it’s better use registerScript function.
SiZE
(Sizemail)
December 19, 2014, 6:48am
8
i wrote this java script in _form where i call this i have no error but it cant displt value in total field.
Yii::app()->clientScript->registerScript('totalAmount','
$(document).ready(function()
{
$("#C, #Cpp").onchange(function(event){
var $subtotal = $("#C").val() + $("#Cpp").val();
$("#Total").val($subtotal);
});
');
Use .change(function(e){}) or .on(‘change’,function(e){}) if you working with jQuery.
To math sum use parseInt or parseFloat funcs cause type of you value may be a string and it makes concatination inspite of calculation.
Also you needn’t to wrap code in $(document).ready(function(){}); And you forget to close ready function callback.
Yii::app()->clientScript->registerScript('totalAmount','
$("#C, #Cpp").change(function(e){
var subtotal = parseInt($("#C").val()) + parseInt($("#Cpp").val());
$("#Total").val(subtotal);
});
');
P.S. Please use [code] for code wrap.
I didn’t wrote that <script> tag should be outside os <html>, just said that he can just place it inside <scrpit> directly in file where he will use js
1 Like
SiZE
(Sizemail)
December 19, 2014, 8:36am
10
I mean
<html>
<head></head>
<body>
<!-- All html code here -->
<script>
// All JS code here
</script>
<!-- else nothing -->
</body>
</html>
That’s exactly how I meant to be