提交資料四種方式

weixin_34253539發表於2016-11-12

1,form提交

<form method="post" action="index.php">
    <input type="text" name="username" value="" />
    <input type="submit" value="提交">
</form>

2,js提交

<form method="post" action="index.php" id="login">
    <input type="text" name="username" id="username" value="" />
    <input type="button" value="提交" onclick="return validate();">
</form>
<script>
function validate() {
    var username = document.getElementsByName('username')[0].value;
    //var username = document.getElementById('username').value;
    if (username == '') {
        return false;
    }
    document.getElementById('login').submit();
}
</script>
  • return vallidate();這裡的return可有可無
  • type="button" 這裡不能用submit,如果用的話,會提交兩次

3,form onsubmit()

<form method="post" action="index.php" onsubmit="return validate();">
    <input type="text" name="username" id="username" value="" />
    <input type="submit" value="提交">
</form>
<script>
function validate() {
    var username = document.getElementsByName('username')[0].value;
    //var username = document.getElementById('username').value;
    if (username == '') {
        return false;
    }
}
</script>

onsubmit必須加上return

4,ajax提交
......

相關文章