javascript确认不会停止提交表单
i am trying to do a confirm function using javascript in php which it worked only in showing the buttons but whatever you press the form will submit heres the code and the function
<form action='Allocated.php' method='post' >
<input type='submit' name='disc' value='Discontue Asset' formnovalidate='formnovalidate' onclick='myFunction()' />
<input type='hidden' name='alloc' value='$AllocID' />
</td>
</form>
and the function
function myFunction() {
var r = confirm("Press a button!");
if (r == true) {
return true;
} else {
return false;
}
}
any help would be appreciated THanks
我正在尝试使用php中的javascript执行确认功能,它只用于显示按钮但是无论你按什么 表单将提交代码和函数 p>
&lt; form action ='Allocated.php'method ='post'&gt;
&lt; input type ='submit 'name ='disc'value ='Discontue Asset'formnovalidate ='formnovalidate'onclick ='myFunction()'/&gt;
&lt; input type ='hidden'name ='alloc'value ='$ AllocID'/&gt ;
&lt; / td&gt;
&lt; / form&gt;
code> pre>
和函数 p>
函数myFunction (){
var r = confirm(“按一个按钮!”);
if(r == true){
return true;
} else {
return false;
}
} \ n code> pre>
任何帮助将不胜感激
THanks p>
div>
Change onclick='myFunction()'
to onsubmit='return myFunction()'
so that the returned value from the function can be returned to the event.
use this:
function myFunction() {
var r = confirm("Press a button!");
if (r) {
return true;
} else {
return false;
}
}
or: for booleans you must use === instead of == :
function myFunction() {
var r = confirm("Press a button!");
if (r == true) {
return true;
} else {
return false;
}
}
or
function myFunction() {
var r = confirm("Press a button!");
if (r === 1) {
return true;
} else {
return false;
}
}
Put this function at attribute onsubmit (with return keyword, thx Scott) on form and change to button
<form action="Allocated.php" method="post" onsubmit="return myFunction()">
<input type="hidden" name="alloc" value="$AllocID" />
<button type="submit">Discontue Asset</button>
</form>
</div>
You can try code below, It's much simpler
Give an id to your form
<form action='Allocated.php' method='post' id="myform">
<input type='submit' name='disc' value='Discontue Asset' formnovalidate='formnovalidate' onclick='myFunction()' />
<input type='hidden' name='alloc' value='$AllocID' />
</td>
Then write your javascript like this
function myFunction() {
var r = confirm("Press a button!");
if (r) {
document.getElementById('myform').submit();
} else {
return false;
}
}