Js 彈出框口并返回值的兩種常用方法
更新時間:2010年12月30日 22:29:00 作者:
有時候我們需要在新窗口執(zhí)行一些代碼并講求將執(zhí)行的結(jié)果返回到這個頁面,那么就需要下面的方法,js常用的就是下面這中方法。
1.window.showModalDialog(url,args,dialogattrs)
參數(shù)說明:
url:彈出頁面地址
agrs:主窗口傳給對話框的參數(shù),可以是任意類型(數(shù)組也可以)
dialogattrs:彈出窗口的樣式參數(shù)
模式對話框用法:
主窗口:var value =window.showModalDialog('test.jsp',strs,'resizable:yes');
彈出框中通過window.returnValue來設(shè)置返回值,上面的value拿到的就是這個值,然后主窗口中可以對
這個值進行處理,實現(xiàn)交互處理
注:模式對話框的應(yīng)用就在于它的返回值,可以返回簡單字符竄,也可以返回數(shù)組,非模式對話框類似
2。window.open:
【父窗口】
<script>
function show_child()
{
var child=window .open("child.html","child","height=200,width=400,status=yes,toolbar=no,menubar=no,location=no");
/* if(!child.closed)
{
if(!window .close())
{
var textValue = frm.txt.value; parent.frm0.txt0.value = textValue;
}
else
{
window .close();
child.close();
}
}*/
}
</script>
<a href="javascript:show_child();">打開子窗口</a>
<form name=frm0>
<input type="text" name="txt0" id="txt0"> //注意這里一定要寫ID屬性不然FF下取不到值
</form>
【子窗口】
<script>
function choseItem()
{
var v="";
var check_item = document.frm.item;
for(i=0;i<check_item.length;i++)
{
if(check_item[i].checked)
{
v+=","+check_item[i].value;
}
document.frm.txt.value=v.replace(/^,{1}/,"");
}
}
function foo()
{
window .close();
window .opener.document.getElementById("txt0").value=document.getElementById("txt").value
}
</script>
<body>
<form name=frm>
<input type=checkbox name=item value=1 onclick="choseItem();">a
<input type=checkbox name=item value=2 onclick="choseItem();">b
<input type=checkbox name=item value=3 onclick="choseItem();">c
<input type=checkbox name=item value=4 onclick="choseItem();">d
<input type=text name="txt" id="txt">
</form>
<input type=button value="關(guān)閉" onclick="foo();">
</body>
小結(jié):一般情況下,windows.open因為自定義的比較多,所以用windows.open的較多,上面的很多網(wǎng)頁編輯器喜歡用showModalDialog,實在不知道用哪個的的,就用window.open吧,很多成熟的cms系統(tǒng)都是用的window.open.
參數(shù)說明:
url:彈出頁面地址
agrs:主窗口傳給對話框的參數(shù),可以是任意類型(數(shù)組也可以)
dialogattrs:彈出窗口的樣式參數(shù)
模式對話框用法:
主窗口:var value =window.showModalDialog('test.jsp',strs,'resizable:yes');
彈出框中通過window.returnValue來設(shè)置返回值,上面的value拿到的就是這個值,然后主窗口中可以對
這個值進行處理,實現(xiàn)交互處理
注:模式對話框的應(yīng)用就在于它的返回值,可以返回簡單字符竄,也可以返回數(shù)組,非模式對話框類似
2。window.open:
【父窗口】
復(fù)制代碼 代碼如下:
<script>
function show_child()
{
var child=window .open("child.html","child","height=200,width=400,status=yes,toolbar=no,menubar=no,location=no");
/* if(!child.closed)
{
if(!window .close())
{
var textValue = frm.txt.value; parent.frm0.txt0.value = textValue;
}
else
{
window .close();
child.close();
}
}*/
}
</script>
<a href="javascript:show_child();">打開子窗口</a>
<form name=frm0>
<input type="text" name="txt0" id="txt0"> //注意這里一定要寫ID屬性不然FF下取不到值
</form>
【子窗口】
復(fù)制代碼 代碼如下:
<script>
function choseItem()
{
var v="";
var check_item = document.frm.item;
for(i=0;i<check_item.length;i++)
{
if(check_item[i].checked)
{
v+=","+check_item[i].value;
}
document.frm.txt.value=v.replace(/^,{1}/,"");
}
}
function foo()
{
window .close();
window .opener.document.getElementById("txt0").value=document.getElementById("txt").value
}
</script>
<body>
<form name=frm>
<input type=checkbox name=item value=1 onclick="choseItem();">a
<input type=checkbox name=item value=2 onclick="choseItem();">b
<input type=checkbox name=item value=3 onclick="choseItem();">c
<input type=checkbox name=item value=4 onclick="choseItem();">d
<input type=text name="txt" id="txt">
</form>
<input type=button value="關(guān)閉" onclick="foo();">
</body>
小結(jié):一般情況下,windows.open因為自定義的比較多,所以用windows.open的較多,上面的很多網(wǎng)頁編輯器喜歡用showModalDialog,實在不知道用哪個的的,就用window.open吧,很多成熟的cms系統(tǒng)都是用的window.open.
相關(guān)文章
深入JavaScript高級程序設(shè)計之對象、數(shù)組(棧方法,隊列方法,重排序方法,迭代方法)
這篇文章主要介紹了深入JavaScript高級程序設(shè)計之對象、數(shù)組(棧方法,隊列方法,重排序方法,迭代方法)的相關(guān)資料,需要的朋友可以參考下2015-12-12JS實現(xiàn)根據(jù)數(shù)組對象的某一屬性排序操作示例
這篇文章主要介紹了JS實現(xiàn)根據(jù)數(shù)組對象的某一屬性排序操作,涉及javascript針對json數(shù)組的遍歷、比較、排序等相關(guān)操作技巧,需要的朋友可以參考下2019-01-01JavaScript 如何計算文本的行數(shù)的實現(xiàn)
這篇文章主要介紹了JavaScript 如何計算文本的行數(shù)的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09利用js將ajax獲取到的后臺數(shù)據(jù)動態(tài)加載至網(wǎng)頁中的方法
今天小編就為大家分享一篇利用js將ajax獲取到的后臺數(shù)據(jù)動態(tài)加載至網(wǎng)頁中的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08JavaScript獲取radio選中值的幾種常用方法小結(jié)
這篇文章主要介紹了JavaScript獲取radio選中值的幾種常用方法,結(jié)合實例形式總結(jié)分析了javascript獲取radio選中值的常見實現(xiàn)方法與操作注意事項,需要的朋友可以參考下2023-06-06