gpt4 book ai didi

javascript - 未经我同意输入类型图像提交表单

转载 作者:太空宇宙 更新时间:2023-11-04 14:22:08 25 4
gpt4 key购买 nike

我有一个这样的输入类型图像:

<input type="image" name="formimage2" width="170" height="37" src="images/tasto-registrati.png" onclick="sub()">

这是 sub():

function sub(){
var mail = document.getElementsByName('mail')[0];
var name = document.getElementsByName('name')[0];
if(mail.value!=""){
bool = true;
}else{
mail.value="Campo obbligatorio";
mail.style.backgroundColor="red";
bool=false;
}
if(name.value!=""){
bool = true;
}else{
name.value="Campo obbligatorio";
name.style.backgroundColor="red";
bool=false;
}
if(bool){
document.form[0].submit();
}

但是每次都提交表单!在这种情况下,我怎么能告诉表格只提交?我尝试:

if(bool){
...
}else{
return false
}

但是什么都没有!你能帮帮我吗??

最佳答案

就用

onclick="return sub()"

我相信当它为 FALSE 时您的表单将不会被提交...

祝你好运!!!

更新 1:

始终在 main 函数中使用 return falsereturn true...

javascript 部分

function checkMe() {

if (Name is incorrect) {
alert(Name is incorrect);
return false;
}


if (Number is incorrect) {
alert(Number is incorrect);
return false;
}
return true;
}

html部分

<input type=submit onClick="return checkMe()">

关于javascript - 未经我同意输入类型图像提交表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9077893/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com