<form name='saveForm' action="updateColumn.aspx" method="post" onkeydown="if(event.keyCode==13){return false}" > 
<input type="hidden" name="tablename" id ="tablename" value="a4"/> <tr class="ttgreen2" style="table-layout: fixed;">
<td><input type="text"  id="id0NotEmpty" name="id"   class="InpKey"  value="0" onfocus="keyfocus(this);" onblur="keyblur(this);"  style="width: 100%"/></td>
<td><input type="text"  id="class0NotEmpty" name="class"   class="InpKey"  value="2" onfocus="keyfocus(this);" onblur="keyblur(this);"  style="width: 100%"/></td>
<td><input type="text"  id="time" name="time"  class="InpKey" size="50" onclick="WdatePicker({dateFmt:'yyyy-MM-dd',isShowClear:true})" value="2012-3-25"    onfocus="keyfocus(this);" onblur="keyblur(this);" style="width: 80%"/></td>
<td><input type="text"  id="price0NotEmpty" name="price"   class="InpKey"  value="32.5400" onfocus="keyfocus(this);" onblur="keyblur(this);"  style="width: 100%"/></td>
<td width="200">
 <input type="text" name="notAutoSubmit"  style="display:none"/>
 <input type="submit" class="cx_btn" id ="0" value="保存"/> 
 <input type="button"  class="cx_btn"  onclick="del('a4','id','0','varchar');"value="删除" /></td>
</tr>
 <input type ="hidden" name ="priname" id ="priname" value ="id">
 <input type ="hidden" name ="pritype" id ="pritype" value ="varchar">
 <input type ="hidden" name ="privalue" id ="privalue" value ="0">
 </form>
   $(":submit").click(function(check) {   代码处理。
     return false;
      alert("hello");
});(1)如何在click事件中阻止表单提交
(2)另外click已经return false 但下面的alert还是执行,没有返回。为什么?

解决方案 »

  1.   

    1)return false可以阻止表单提交,未阻止只能说return false前面的代码出错了2)这种问题不可能发生
      

  2.   

    $(":submit").click(function(check) {var result ;
    省略部分
               if (idvalue != Currentvalue) {
                    $.getJSON("checkIdExist.aspx", { state: "checkId", idColName: pName, idColType: pType, inputid: Currentvalue, tname: tablename }, function(data) {
                        if (data.ckResult == "no") {
                            alert("修改主键重复,请换一个主键再保存!");
                            //  check.preventDefault();                                       result = false;
                        }
                    })
                }
                if (result == false) {
                    check.preventDefault();                              return result;
                }
                                alert("hello");

    这样写对吗?
      

  3.   

    $.getJSON发送的ajax请求时异步的,所以会直接执行到
    if (result == false) {
    check.preventDefault(); return result;
    }
    这句了,由于result定义的时候未赋值,result==false不成立,所以执行alert('hello'),然后提交了表单将$.getJSON改为$.ajax,$.ajax可以配置请求为同步的,你的这个要求必须同步执行    $(":submit").click(function (check) {
            var result;
            //省略部分
            if (idvalue != Currentvalue) {
                /*$.getJSON("checkIdExist.aspx", { state: "checkId", idColName: pName, idColType: pType, inputid: Currentvalue, tname: tablename }, function (data) {
                    if (data.ckResult == "no") { alert("修改主键重复,请换一个主键再保存!"); check.preventDefault(); result = false; }
                });*/            $.ajax({
                    url: "checkIdExist.aspx"
                    , data: { state: "checkId", idColName: pName, idColType: pType, inputid: Currentvalue, tname: tablename }
                    , async: false//////////////同步
                    , success: function (data) {
                        if (data.ckResult == "no") {
                            alert("修改主键重复,请换一个主键再保存!");
                            check.preventDefault();
                            result = false; 
                        }
                    }
                    , error: function (xhr) { alert('shit,动态页出错鸟~~'); }
                });
            }
            if (result == false) { check.preventDefault(); return result; }
            alert("hello");
        });
      

  4.   

    才看到这个回答,后来调试才知道是同步问题,不过getJson也可以同步的,非常感谢!