下面的代码直接用<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>

解决方案 »

  1.   

    onClick="Create()" 有了绑定事件,为什么还要去捕获onclick事件?
    还有表单阻止提交一般是用return false
      

  2.   

    <form name="form" action="?action=ok">
        <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>
      

  3.   

                alert("提交成功!");
                return true;//更正