我想在不刷新页面的情况下显示名为'totCal'的下一个输入框中'txtCal'输入框中输入的值的总和。如何使用Javascript获取3个输入框值的总和?
paper.php
<label>No of Questions - Easy level</label>
<input type="number" class="txtCal" id="input_item" name="level1_no_of_questions" placeholder="No of Questions - Easy level" onblur="findTotal()" Required/><br />
<label>No of Questions - Intermediate level</label>
<input type="number" name="level2_no_of_questions" class="txtCal" id="input_item" placeholder="No of Questions - Intermediate level" onblur="findTotal()" Required/><br />
<label>No of Questions - Difficult level</label>
<input type="number" name="level3_no_of_questions" class="txtCal" id="input_item" placeholder="No of Questions - Difficult level" onblur="findTotal()" Required/><br/>
<label>Total No of Questions</label>
<input type="number" name="total_no_of_questions" class="totCal" value="20" readonly="readonly" id="input_item" placeholder="Total No of Questions max="20"/><br />
<input type="submit" value="Add Exam Paper" name="submit" id="submit"/>
<form/>
操作页面
question.php
<?php
if(isset($_POST['submit'])){
$level1_no_of_questions=$_POST['level1_no_of_questions'];
$level2_no_of_questions=$_POST['level2_no_of_questions'];
$level3_no_of_questions=$_POST['level3_no_of_questions'];
$total_no_of_questions=$_POST['total_no_of_questions'];
$sql2= "INSERT INTO exam_paper (level1_no_of_questions,level2_no_of_questions,level3_no_of_questions,total_no_of_questions) VALUES ('$level1_no_of_questions','$level2_no_of_questions','$level3_no_of_questions','$total_no_of_questions');
if (mysqli_query($dbcon,$sql2)){
//Redirection to the this page
header("Location:paper.php");
exit();
}else{
$error=mysqli_error($dbcon);
}
}
}
?>
<script type="text/javascript">
function findTotal(){
var arr = document.getElementsByClassName('txtCal');
var tot=0;
for(var i=0;i<arr.length;i++){
if(parseInt(arr[i].value))
tot += parseInt(arr[i].value);
}
document.getElementByClassName('totCal').value = tot;
}
</script>
这不就是working..What错误?我不能使用输入ID或和name.names去sql和id去设计..
意味着什么呢'not'工作? –
当我把这些值放入3个输入时,它不会给出totalinput字段中的总数 –
这些字段从哪里获取数据并不重要;你只能通过'onblur()'调用JavaScript。当你尝试运行'onblur()'时,你的领域**已经有**数据了吗?在这种情况下,你可以通过一个简单的'console.log()'找到错误。 –