需求:
1、點擊添加彈出模態框;
2、點擊編輯彈出模態框,並有默認值;
3、點擊刪除,刪除此行數據。
<!-- jquery模態框 --> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> .hide{ display:none; } .modal{ position:fixed; left:50%; top:50%; width:500px; height:400px; margin-left:-200px; margin-top:-250px; z-index:10; background-color:white; } .shade{ position:fixed; left:0; right:0; top:0; bottom:0; opacity:0.6; background-color:black; z-index:9; } p{ text-align:center; } </style> </head> <body> <input type="button" value="添加"> <div class="modal hide"> <p>地址:<input type="text"></p> <p>端口:<input type="text"></p> <p><input type="button" value="取消"></p> </div> <div class="shade hide"></div> <div class="container"> <table border="1"> <tr> <td>1.1.1.1</td> <td>80</td> <td><input type="button" value="編輯"> | <input type="button" value="刪除"></td> </tr> <tr> <td>1.1.1.2</td> <td>81</td> <td><input type="button" value="編輯"> | <input type="button" value="刪除"></td> </tr> <tr> <td>1.1.1.3</td> <td>82</td> <td><input type="button" value="編輯"> | <input type="button" value="刪除"></td> </tr> <tr> <td>1.1.1.4</td> <td>83</td> <td><input type="button" value="編輯"> | <input type="button" value="刪除"></td> </tr> </table> </div> <script src="jquery-1.12.4.js"></script> <script> //添加按鈕 $('input[value="添加"]').click(function(){ $('.hide').removeClass('hide'); }); //取消按鈕 $('input[value="取消"]').click(function(){ $('input[type="text"]').val(''); $('.modal,.shade').addClass('hide'); }); //編輯按鈕 $('input[value="編輯"]').click(function(){ $('.hide').removeClass('hide'); var tds = $(this).parent().prevAll(); //jquery對象加上索引是dom對象 $($('.modal input')[0]).val($(tds[1]).text()); $($('.modal input')[1]).val($(tds[0]).text()); }); //刪除按鈕 $('input[value="刪除"]').click(function(){ $(this).parent().parent().remove(); }); </script> </body> </html>
版本二:
可擴展的版本,如需添加一列數據,不用更改js代碼
<!-- jquery模態框可擴展版本 --> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> .hide{ display:none; } .modal{ position:fixed; left:50%; top:50%; width:500px; height:400px; margin-left:-200px; margin-top:-250px; z-index:10; background-color:white; } .shade{ position:fixed; left:0; right:0; top:0; bottom:0; opacity:0.6; background-color:black; z-index:9; } p{ text-align:center; } </style> </head> <body> <input type="button" value="添加"> <div class="container"> <table border="1"> <tr> <td target="hostname">1.1.1.1</td> <td target="port">80</td> <td target="ip">192.168.1.101</td> <td><input type="button" value="編輯"> | <input type="button" value="刪除"></td> </tr> <tr> <td target="hostname">1.1.1.2</td> <td target="port">81</td> <td target="ip">192.168.1.102</td> <td><input type="button" value="編輯"> | <input type="button" value="刪除"></td> </tr> <tr> <td target="hostname">1.1.1.3</td> <td target="port">82</td> <td target="ip">192.168.1.103</td> <td><input type="button" value="編輯"> | <input type="button" value="刪除"></td> </tr> <tr> <td target="hostname">1.1.1.4</td> <td target="port">83</td> <td target="ip">192.168.1.104</td> <td><input type="button" value="編輯"> | <input type="button" value="刪除"></td> </tr> </table> </div> <div class="modal hide"> <p>主機:<input name="hostname" type="text"></p> <p>端口:<input name="port" type="text"></p> <p>地址:<input name="ip" type="text"></p> <p><input type="button" value="取消"></p> </div> <div class="shade hide"></div> <script src="jquery-1.12.4.js"></script> <script> //添加按鈕 $('input[value="添加"]').click(function(){ $('.hide').removeClass('hide'); }); //取消按鈕 $('input[value="取消"]').click(function(){ $('input[type="text"]').val(''); $('.modal,.shade').addClass('hide'); }); //編輯按鈕 $('input[value="編輯"]').click(function(){ $('.hide').removeClass('hide'); //獲取點擊前面標簽 var tds = $(this).parent().prevAll(); tds.each(function(){ //獲取target值 var tar_val = $(this).attr('target'); //獲取當前td的內容 var con = $(this).text(); //根據target尋找modal中的對應框,並寫入內容 $('.modal input[name="'+tar_val+'"]').val(con); }); }); //刪除按鈕 $('input[value="刪除"]').click(function(){ $(this).parent().parent().remove(); }); </script> </body> </html>