有兩種方式,第一種方法是我本身作出來的,對button類型的input用onclick()方法,觸發js代碼,在js裏邊進行數據驗證,以及從新給action賦值。javascript
jsp頁面中的onclick屬性定義:html
<td><input id="ud" type="button" value="修改" onClick="split()" /></td> <td><input id="dd" type="button" value="刪除" onClick="deleteConfirm()" /></td> <td><input id="rd" type="button" value="角色維護" onClick="split2()" /></td>
如下是js代碼:java
<script type="text/javascript"> function split(){ var checks = document.getElementsByName("check"); //經過input的name屬性獲取對象集合 var n = 0; for(i=0;i<checks.length;i++){ if(checks[i].checked) //用循環檢查是否被選中 n++; //若是被選擇則自加一 } if(n==0){ //條件判斷 alert("請選擇一項數據進行修改!");} else if(n>=2){ alert("請選擇一項進行修改!");} else if(n==1){ var f = document.getElementById("idselect"); //符合條件則獲取form表單對象 f.action ="updateUser.crud"; //更改formaction f.submit(); //提交form表單 } } </script> <script type="text/javascript"> function split1(){ var f = document.getElementById("idselect"); f.action = "deleteUser.crud"; f.submit(); } </script> <script type="text/javascript"> function split2(){ var x=document.getElementById("rd"); if(x.value=="角色維護"){ var f = document.getElementById("idselect"); f.action ="roleUser.crud"; f.submit(); } } </script> <script type="text/javascript"> function deleteConfirm() { if(confirm("確認要刪除? 刪除後將不可更改")) { return split1(); }else{ return false; } } function deleteConfirm2() { if(confirm("確認要修改?")) { return split(); }else{ return false; } } </script>
第二種方法是h5的新方法,能夠多個引用:jsp
<!DOCTYPE html> <html> <head> <title>form</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <form id="addForm" method="get" oninput="a.value=color.value;b.value=range.value"> Username: <input type="text" name="username" form="addForm" placeholder="Please input your username." style="width: 200px;" /><br> Password: <input type="password" name="password" form="addForm" placeholder="Please input your password." autofocus="true" style="width: 200px;" /><br> Please entry the book: <input type="text" name="book" list="books" /><br> Select color: <input id="color" type="color" name="color" /><br> Select date: <input type="date" name="date" /> Select time: <input type="time" /><br> Select datetime: <input type="datetime-local" name="datetime-local" /><br> Select week: <input type="week" name="week" /> Select month: <input type="month" name="month" /><br> Please entry your e-mail: <input type="email" multiple="multiple" name="email" /><br> Please entry your tel-number: <input type="tel" name="tel" /><br> Please entry an URL: <input type="url" name="url" /><br> Must entry number: <input type="number" name="number" /><br> A range bar: <input type="range" name="range" min="0" max="100" step="5" /><br> Search bar: <input type="search" name="search" /><br> <output name="a" for="color"></output><br> <output name="b" for="range"></output> </form> <input type="submit" value="submit" form="addForm" formaction="index.html" /> <input type="submit" value="submit2" form="addForm" formaction="h501.html" /><br> <datalist id="books"> <option value="aaa">AAA</option> <option value="bbb">BBB</option> <option value="ccc">CCC</option> </datalist> </body> </html>