我正在尝试使用JQuery计算百分比和加法。添加可以正常运行,但百分比计算不能。我的代码如下。 如何更改它并计算百分比?
index.php
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<br/><br />
<form id="jquery_percentage">
<label>Total Marks</label>
<input type="text" name="total_marks" id="total_marks"/>
<br/>
<br/>
<label>Written Marks</label>
<input type="text" name="written_marks" id="written_marks"/>
<br/>
<br/>
<label>Viva Marks</label>
<input type="text" name="viva_marks" id="viva_marks"/>
<br/>
<br/>
<label>Grooming Marks</label>
<input type="text" name="grom_marks" id="grom_marks"/>
<br/>
<br/>
<label>Obtain Marks</label>
<input type="text" name="obt_marks" id="obt_marks"/>
<br/>
<br/>
<label>Percentage Marks</label>
<input type="text" name="perc_marks" id="perc_marks"/>
<br/>
<br/>
<input type="button" name="submit" id="submit" value="Submit" />
</form>
<div id="return"></div>
</body>
</html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script>
$('#written_marks, #grom_marks, #viva_marks').change(function(){
var written_marks = parseFloat($('#written_marks').val()) || 0;
var grom_marks = parseFloat($('#grom_marks').val()) || 0;
var viva_marks = parseFloat($('#viva_marks').val()) || 0;
$('#obt_marks').val(written_marks + viva_marks + grom_marks);
});
</script>
<script>
$('#total_marks, #obt_marks').change(function(){
var total_marks = parseFloat($('#total_marks').val());
var obt_marks = parseFloat($('#grom_marks').val());
//var viva_marks = parseFloat($('#viva_marks').val()) || 0;
$('#perc_marks').val((obt_marks * 100) / total_marks);
});
</script>
答案 0 :(得分:0)
当您实际转到元素并更改其值时,会发生onChange事件。如果您只是动态更改它的值,它不会自动触发。在这种情况下,您必须触发此事件the-pate.movie.com
sin-city.movie.com
batman.movie.com
etc...
。检查代码段
$("#obt_marks").trigger("change");
&#13;