jQuery提交Form表單的正確方式
阿新 • • 發佈:2019-01-01
昨天寫了兩個form表單表示式,發現無論如何都提交不過去。後面網上查到了原因,那就是不要省略寫定義方法,必須要把$(document).ready補全,不然就會發現表單提交永遠是第一個. 看程式碼:script程式碼: <pre name="code" class="html"><script type="text/javascript"> //為查詢按鈕新增點選事件,傳送資訊給後臺 $(document).ready(function(){ $('#searchbutton').click(function(){ var info = $('#outputuserInfo').val(); $('#mysearchUserId').submit(); }); }); </script>
html程式碼:<pre name="code" class="html"><form action="searchForUserId" method ="get" id="mysearchUserId"> <input type="button" value="新增"id="addEmployment"/> 請輸入使用者名稱: <input type="text" id="outputuserInfo" name="searchid" style="color:red" /> <input type="button" value="查詢" id="searchbutton"/> <span id="showMessage" style="color:red" /></span> </form>
</pre><p></p><p></p><pre name="code" class="html"> script程式碼:<script type="text/javascript"> //為查詢按鈕新增點選事件,傳送資訊給後臺 $(document).ready(function(){ $('#findaInfo').click(function(){ alert("wo dian ji le tian jia an niu "); $('#addInfoForm').submit(); }); }); </script>
html程式碼:
<div id="showEmployment" align="center">
<form action="addStoreAstntSusakeList" method="get" id="addInfoForm">
<table >
<tr>
<td>請輸入使用者名稱:</td>
<td><input type="text" id="PassUser" name="addInfo"></td>
<td><span id="showMessage1" style="color:red" /></span><td>
</tr>
<tr>
<td></td>
<td>
<input type="button" value="確定" id="findaInfo"/>
<input type="button" value="返回" id="ReturnInfor"/>
</td>
<td></td>
</tr>
</table>
</form>
</div>
這個地方一定要注意,補全定義方法。不然按ID去提交,是實現不了的。即使你用$('form').submit()或者$('form:first').submit()去提交,永遠只能提交第一個。