下面的代码直接用<input name="submit" type="submit" value="提交">可以阻止表单提交。现在我想用图片按钮<input type="image" border="0" name="submit" src="AnNiu.png" alt="提交" onClick="Create()">提交,却不会写相应的阻止代码。求指教:<form name="form">
<input name="n" value="2">
<input name="submit" type="submit" value="提交">
<input type="image" border="0" name="submit" src="AnNiu.png" alt="提交" onClick="Create()">
</form>
<script>
if(document.all){//ie
document.form.submit.attachEvent("onclick",checkform_success);
}else{//ff
document.form.submit.addEventListener("click",checkform_success,false);
};
function checkform_success(e){
e=e||window.event;
if(document.form.n.value>1){
alert("禁止提交!");
if(document.all) e.returnValue=false;//ie,window.event.returnValue=false阻止元素默认行为
else e.preventDefault();//ff,event.preventDefault阻止元素默认行为
}
}
function Create(){
document.form.n.value = "提交成功";
};
</script>
<input name="n" value="2">
<input name="submit" type="submit" value="提交">
<input type="image" border="0" name="submit" src="AnNiu.png" alt="提交" onClick="Create()">
</form>
<script>
if(document.all){//ie
document.form.submit.attachEvent("onclick",checkform_success);
}else{//ff
document.form.submit.addEventListener("click",checkform_success,false);
};
function checkform_success(e){
e=e||window.event;
if(document.form.n.value>1){
alert("禁止提交!");
if(document.all) e.returnValue=false;//ie,window.event.returnValue=false阻止元素默认行为
else e.preventDefault();//ff,event.preventDefault阻止元素默认行为
}
}
function Create(){
document.form.n.value = "提交成功";
};
</script>
解决方案 »
- 求帮忙 js
- jquery清空和回填input type="text"
- 如何分析JavaScript函数的调用关系?
- jquery循环bind事件的问题
- 请问iframe父窗口调用跳转后的子窗口里面的方法该怎么实现?
- jsp 怎么才能把word文件 按原来的格式显示在页面中?
- replace 语法?var k = 'abc' str.replace( k, '<$0>' ); 这个语法好象不对,该怎么写?
- 高分求救了!!!请问我要做个当鼠标双击文本框时就可弹出时间选择器,可选着年份和日期。
- 小问题-100分 都来拿分啊!!!!!!!
- 怎样才能用js(客户端)检测表单中输入框的长度啊?
- 关于JS代码兼容360的问题
- Extjs 4.1 button click 事件和 cellediting的edit事件
还有表单阻止提交一般是用return false
<input name="n" value="2">
<input type="image" name="submit" src="AnNiu.png" value="提交" onClick="return Create()">
</form>
<script>
function Create(e){
var n=parseFloat(document.getElementsByName("n")[0].value);
if(n>1){
alert("禁止提交!");
return false;
}else{
alert("提交成功!");
return false;
}
}
</script>
return true;//更正