[英]Alert to close child windows before form submission
我在表單中有一個表數據,每行都有動態鏈接,在新窗口中打開一個外部鏈接(新的瀏覽器選項卡)。 這些行是帶有動態ID的n個數字。
我需要的是,當用戶嘗試提交表格詳細信息時,他們必須關閉所有子窗口。 如果仍然打開任何子div並且如果他試圖提交表單,則應該給出關閉子窗口的警報
這是表單的示例DEMO
<form>
<table border="1">
<tr>
<td><input type="text" /></td>
<td>SC1</td>
<td><a href="http://www.google.com" target="_blank">this is an external link</a></td>
<tr>
<td><input type="text" /></td>
<td>SC2</td>
<td><a href="http://www.google.com" target="_blank">this is an external link</a></td>
<tr>
<td><input type="text" /></td>
<td>SC3</td>
<td><a href="http://www.google.com" target="_blank">this is an external link</a></td>
</tr>
</table>
<input type="submit" value="submit" />
</form>
您可以將每個打開的窗口存儲在一個數組中,並在用戶嘗試提交表單時檢查打開的窗口:
var windows = [];
$('a').click(function () {
var myWindow = window.open(this.href);
windows.push(myWindow);
});
$('form').submit(function () {
for (var i = 0; i < windows.length; i++) {
if (!windows[i].closed) {
alert('Close all windows before continuing');
return false;
}
}
});
看起來你需要跟蹤打開的窗口並在提交時關閉它們
var windows = [];
$('a').click(function(){
var win = window.open($(this).attr('href'),"_blank");
windows.push(win);
});
$('input[type="submit"]').click(function(){
if(windows && windows.length > 0)
$.each(windows,function(index,el){
el.close();
});
});
這里是你可以使用的小提琴http://jsfiddle.net/JpDhX/4/
1.Instead of <a href> use onclick javascript:window.open.
2.Give title to the window.
3.For closing
if (document.title === "the title you want") {
window.close();
}
4.Give the above code in button Click.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.