js取消提交表單

<script language="javascript" type="text/javascript">
    function check(){
        var begin_address =  document.getElementById("begin_address").value;
        var end_address =  document.getElementById("end_address").value;
        //刪除所以空格
        begin_address = begin_address.replace(" ","");
        end_address = end_address.replace(" ","");
        if(begin_address == ""){
            alert("始發站不能爲空");
            return false;
        }
        if(end_address == ""){
            alert("終點站不能爲空");
            return false;
        }
        document.getElementById("begin_address").value = begin_address;
        document.getElementById("end_address").value = end_address;
        return true
    }

</script>
<form onsubmit="return check()" action="" method="post">
    <table id="func">
        <tr>
            <td>
                <img id="img_head" src="head.jpg" />
            </td>
        </tr>
        <tr>
            <td>
                <div id="t">
                    <input id = "begin_address"  class="address" type="text" name = "begin_address" placeholder="添加始發站名"/>
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <input id = "end_address" class="address" type="text" name = "end_address" placeholder="添加終點站名"/>
            </td>
        </tr>
        <tr>
            <td align="center">
                <input id="add" type="submit" value="添加"/>
            </td>
        </tr>
    </table>
</form>

注意:
1.要在onsubmit那寫return.
2.要在檢測函數裏返回false(阻止提交返回false,不阻止返回true)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章