我有一个表单,想要在按下提交按钮后执行此功能:
function bereken() {
if(document.getElementById('v1_1').checked) {
c = c - 0
}else if(document.getElementById('v1_2').checked) {
c += 1;
}else if(document.getElementById('v1_3').checked) {
c += 2;
}else if(document.getElementById('v1_4').checked) {
c += 3;
}else if(document.getElementById('v1_5').checked){
c += 4;
}
var naam = document.getElementById('naamTxt').value;
var percentages = "C: " + c/40*100 + "%. S: " + s/40*100 + "%. G: "+ g/40*100 + "%. E:" + e/20*100 + "%. I= " + i/20*100 + "%";
var antwoorden = "1. Ik ben spontaan =" + $('input[name=v1]:checked', '#myForm').val() + " 2. Mijn gedachten zijn ordelijk = " + $('input[name=v1]:checked', '#myForm').val();
var li = document.getElementById("klasDown");
var klas = li.options[li.selectedIndex].value;
alert(klas + ", "+ naam + " = C: " + c/40*100 + "%. S: " + s/40*100 + "%. G: "+ g/40*100 + "%. E:" + e/20*100 + "%. I= " + i/20*100 + "% " + "1. Ik ben spontaan =" + $('input[name=v1]:checked', '#myForm').val() + " 2. Mijn gedachten zijn ordelijk = " + $('input[name=v1]:checked', '#myForm').val());
}
我尝试了不同的方法,通过本网站上的类似问题进行探索,但没有成功。有人能指出为什么它没有被解雇?
<form id="myForm" method="post" onsubmit="return bereken()" >
Volledige naam: <input name="naamTxt" type="text" maxlength="512" id="naamTxt" class="searchField"/> <br>
Klas: <select name="klasDown">
<option value="H4A" selected="selected">H4A</option>
<option value="H4B" >H4B</option>
<option value="H4C">H4C</option>
<option value="H4C">H4D</option>
<option value="H4C">V4A</option>
<option value="H4C">V4B</option>
<option value="H4C">V4C</option>
<option value="H4C">V4D</option>
</select>
</div>
<br> <input type="submit" name="submit" value="submit" onclick="bereken()">
</form>
我试过onsubmit,onclick等..
答案 0 :(得分:3)
这种情况正在发生,因为您不会因return
或false
使用true
或function [...](e) {
e.preventDefault();
[...]
}
来阻止浏览器的默认行为。
<input type="submit" name="submit" value="submit" onclick="bereken()">
更改,
<input type="submit" name="submit" value="submit" onclick="bereken(event)">
要,
{{1}}
传递内联事件侦听器的事件参数。
答案 1 :(得分:-1)
我认为如果你使用jQuery,这应该可行。
$("#myForm").submit(function(e){
e.preventDefault();
// your code here
});