html表单按钮重定向到同一页面

我的页面中有2个按钮。我只想重定向到页面,只有当我点击按钮。但是我的代码将从两者和按钮重定向到该页面。Addform.phpsubmitsubmitreset


<form id= "myForm" action= "Addform.php" method= "post">

                    First name:

                    <input type="text" id="fname"><br/><br/>

                    Last name:

                    <input type="text" id="lname"><br/><br/>

                    Age:

                    <input type="text" id="age"><br/><br/>

                    Major Subject:

                    <select id="select">

                      <option value="">---</option>

                      <option value="op1">Mathematics</option>

                      <option value="op2">Computer</option>

                      <option value="op3">Statistics</option>

                    </select><br/><br/>

                    GPA:

                    <input type="text" id="gpa"><br/><br/>


                <button id="btnSubmit" onclick="validation()" value= "submit">Submit</button>

                <button id="btnReset" onclick= "resetForm()">Reset</button><br/>

            </form>


沧海一幻觉
浏览 158回答 2
2回答

Qyouu

你必须像这样编码,他们做的按钮类型提交表单简单的方式是设置像这样的按钮类型type = "reset"<form id= "myForm" action= "Addform.php" method= "post">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; First name:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" id="fname"><br/><br/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Last name:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" id="lname"><br/><br/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Age:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" id="age"><br/><br/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Major Subject:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <select id="select">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="">---</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="op1">Mathematics</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="op2">Computer</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="op3">Statistics</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </select><br/><br/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GPA:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" id="gpa"><br/><br/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button type="button" id="btnSubmit" onclick="validation()" value= "submit">Submit</button>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button type="button" id="btnReset" onclick= "resetForm()">Reset</button><br/><!-- or --><!--<button type="reset" >Reset</button>-->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </form>

繁星coding

button默认情况下提交表单。请改用:input<input type="button" id="btnSubmit" onclick="validation()" value= "Submit"><input type="button" id="btnReset" onclick= "resetForm()" value="Reset">或者,我认为您仍然可以使用按钮,但您必须阻止默认行为。使用普通的javascript:document.getElementsByTagName("button").forEach((item, index)=>{&nbsp; &nbsp; item.addEventListener('onclick', function(event){&nbsp; &nbsp; &nbsp; &nbsp; event.preventDefault()&nbsp; &nbsp; });})使用 jQuery:$('button').click(function(event){&nbsp; &nbsp; event.preventDefault();});
打开App,查看更多内容
随时随地看视频慕课网APP