使用JQuery提交表单时,请检查HTML中标记为必填的字段

我正在使用Jquery.submit()函数使用JQuery提交表单。但是我有些字段标记为requiredHTML。.submit()提交表格之前,我应如何使用此功能来检查必填字段?



这是代码


<script>

function submit() {

    $("form").submit();

}


function signup() {

    alert("You're going to sign up");

}

</script>


<form action="JavaScript:void(0);" onsubmit="signup()" method="post">

    <div class="input">

        <label for="email">Email:</label>

        <input required="" id="email"/>

    </div>

    <div class="input">

        <label for="password">Password:</label>

        <input required="" type="password" id="password"/>

    </div>


    <a class="submit" href="JavaScript:submit();">Sign Up</a>

    <button type="submit" class="none_display"></button>

</form>

a由于某些浏览器中buttonandinput标记的样式更改,我需要使用标记提交表单。


皈依舞
浏览 361回答 3
3回答

慕桂英546537

您发生了一些不必要的事情。您不需要action="JavaScript:void(0);"表格。去掉 <a class="submit" href="JavaScript:submit();">Sign Up</a>function signup() {&nbsp; &nbsp; alert("You're going to sign up");}<form onsubmit="signup()">&nbsp; &nbsp; <div class="input">&nbsp; &nbsp; &nbsp; &nbsp; <label for="email">Email:</label>&nbsp; &nbsp; &nbsp; &nbsp; <input required="" id="email"/>&nbsp; &nbsp; </div>&nbsp; &nbsp; <div class="input">&nbsp; &nbsp; &nbsp; &nbsp; <label for="password">Password:</label>&nbsp; &nbsp; &nbsp; &nbsp; <input required="" type="password" id="password"/>&nbsp; &nbsp; </div>&nbsp; &nbsp; <button type="submit" class="none_display">Sign up</button></form>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript