0

Javascript操作下拉框的常用方法

by 刘武 3. 十一月 2009 22:43

项目中碰到需要用javascript操作下拉框的情况,顺便做一下总结,列出一些常用方法,以下方法均在FIRFOX3.5及IE8上测试过,如有其他浏览器无法正常运行的请与笔者联系。

/*添加一个下拉框*/
function AddDropDownList(id,fatherCtl)
{
    if(!document.getElementById(id))
    {
        var ddl = document.createElement('select');
        ddl.setAttribute("id",id);  
        if(fatherCtl&&document.getElementById(fatherCtl))
            document.getElementById(fatherCtl).appendChild(ddl);
        else
            document.body.appendChild(ddl);
     }
}

/*删除指定的下拉框*/
function RemoveDropDownList(id)
{
      var ctl = document.getElementById(id);
      if(ctl)
        ctl.parentNode.removeChild(ctl);
}

/*给下拉框添加选项*/
function AddDDDLOption(id,text,value)
{
    var ctl = document.getElementById(id);
    if(ctl)
    {
        ctl.options[ctl.options.length]   =   new   Option(text,value); 
    }
}

/*删除所有选项*/
function RemoveAllDDLOptions(id)
{
    var ctl = document.getElementById(id);
    if(ctl)
    {
        ctl.options.length=0;
    }
}

/*删除指定索引的选项*/
function RemoveDDLOption(id,index)
{
    var ctl=document.getElementById(id);
    if(ctl && ctl.options[index])
    {
        ctl.options[index]=null;
    }
}

/*获取下拉框选择的值*/
function GetDDLSelectedValue(id)
{
    var ctl = document.getElementById(id);
    if(ctl)
    {
        return ctl.options[ctl.selectedIndex].value;
    }
}

/*获取下拉框选择的文本*/
function GetDDLSelectedText(id)
{
    var ctl = document.getElementById(id);
    if(ctl)
    {
        return ctl.options[ctl.selectedIndex].text;
    }
}

Tags:

技术生涯

0

Javascript怎么在两个窗体之间传值2-showModalDialog

by 刘武 23. 二月 2009 22:12

前一篇文章Javascript怎么在两个窗体之间传值中讲到了如何利用window.open()方法打开新窗体,并在两个窗体之间传值的方法。javascript中还有一个函数window.showModalDialog也可以打开一个新窗体,不过他打开的是一个模态窗口,那么如何在父窗体和子窗体之间传值呢?我们先看该函数的定义:vReturnValue = window.showModalDialog(sURL [, vArguments] [,sFeatures])   

参数说明: 

sURL--必选参数,类型:字符串。用来指定对话框要显示的文档的URL。 

vArguments--可选参数,类型:变体。用来向对话框传递参数。传递的参数类型不限,包括数组等。对话框通过window.dialogArguments来取得传递进来的参数。

sFeatures--可选参数,类型:字符串。用来描述对话框的外观等信息,可以使用以下的一个或几个,用分号“;”隔开。   

  1. dialogHeight :对话框高度,不小于100px,IE4中dialogHeight 和 dialogWidth 默认的单位是em,而IE5中是px,为方便其见,在定义modal方式的对话框时,用px做单位。
  2. dialogWidth: 对话框宽度。 
  3. dialogLeft: 离屏幕左的距离。
  4. dialogTop: 离屏幕上的距离。 
  5. center: {yes | no | 1 | 0 }:窗口是否居中,默认yes,但仍可以指定高度和宽度。
  6. help: {yes | no | 1 | 0 }:是否显示帮助按钮,默认yes。   
  7. resizable: {yes | no | 1 | 0 } [IE5+]:是否可被改变大小。默认no。
  8. status: {yes | no | 1 | 0 } [IE5+]:是否显示状态栏。默认为yes[ Modeless]或no[Modal]。
  9. scroll:{ yes | no | 1 | 0 | on | off }:指明对话框是否显示滚动条。默认为yes。

如:"dialogWidth=200px;dialogHeight=100px"

因此我们可以通过window.dialogArguments参数来在两个窗体之间传值

如下面两个页面:FatherPage.htm:


<script type="text/javascript">
function OpenChildWindow()
{
    window.showModalDialog('ChildPage.htm',document.getElementById('txtInput').value);
}
</script>
<input type="text" id="txtInput" />
<input type="button" value="OpenChild" onclick="OpenChildWindow()" />

ChildPage.htm:


<body onload="Load()">
<script type="text/javascript">
function Load()
{
    document.getElementById('txtInput').value=window.dialogArguments ;
}
</script>
<input type="text" id="txtInput" />
</body>

 

上面只是传递简单的字符串,我们还可以传递数组,如:FatherPage.htm:


<script type="text/javascript">
function OpenChildWindow()
{
    var args = new Array();
    args[0] = document.getElementById('txtInput').value;
    window.showModalDialog('ChildPage.htm',args);
}
</script>
<input type="text" id="txtInput" />
<input type="button" value="OpenChild" onclick="OpenChildWindow()" />
ChildPage.htm:

<script type="text/javascript">
function Load()
{  
    document.getElementById('txtInput').value=window.dialogArguments[0] ;
}
</script>

 

同样我们还可以传递对象,如:FatherPage.htm:


<script type="text/javascript">function OpenChildWindow(){   var obj = new Object();   obj.name = document.getElementById('txtInput').value;    window.showModalDialog('ChildPage.htm',obj);}</script><input type="text" id="txtInput" /><input type="button" value="OpenChild" onclick="OpenChildWindow()" />
ChildPage.html:

<script type="text/javascript">function Load(){       var obj = window.dialogArguments;    document.getElementById('txtInput').value=obj.name ;}</script>

 

以上都是从父窗体向子窗体传值,那么如何从子窗体向父窗体传值呢 ?其实通过window.returnValue就可以获取子窗体的值,window.returnValuewindow.dialogArguments一样,可以是任意变量,包括字符串,数组,对象等。如:FatherPage.html:


<script type="text/javascript">function OpenChildWindow(){   var obj = new Object();   obj.name = document.getElementById('txtInput').value;   var result = window.showModalDialog('ChildPage.htm',obj);   document.getElementById('txtInput').value = result.name;}</script><input type="text" id="txtInput" /><input type="button" value="OpenChild" onclick="OpenChildWindow()" />
ChildPage.html:

<body onload="Load()"><script type="text/javascript">function Load(){       var obj = window.dialogArguments;    document.getElementById('txtInput').value=obj.name ;}function SetValue(){    var obj = new Object();    obj.name =  document.getElementById('txtInput').value;    window.returnValue = obj;    window.close();}</script><input type="text" id="txtInput" /><input type="button" value="SetFather" onclick="SetValue()" /></body>

Tags: , ,

技术生涯

0

Javascript怎么在两个窗体之间传值

by 刘武 16. 二月 2009 21:28

众所周知window.open() 函数可以用来打开一个新窗口,那么如何在子窗体中向父窗体传值呢,其实通过window.opener即可获取父窗体的引用。

如我们新建窗体FatherPage.htm:


<script type="text/javascript">function OpenChildWindow(){    window.open('ChildPage.htm');   }</script><input type="text" id="txtInput" /><input type="button" value="OpenChild" onclick="OpenChildWindow()" />

然后在ChildPage.htm中即可通过window.opener来访问父窗体中的元素:


<script type="text/javascript">function SetValue(){    window.opener.document.getElementById('txtInput').value        =document.getElementById('txtInput').value;    window.close();}</script><input type="text" id="txtInput" /><input type="button" value="SetFather" onclick="SetValue()" />

其实在打开子窗体的同时,我们也可以对子窗体的元素进行赋值,因为window.open函数同样会返回一个子窗体的引用,因此FatherPage.htm可以修改为:


<script type="text/javascript">function OpenChildWindow(){    var child = window.open('ChildPage.htm');      child.document.getElementById('txtInput').value        =document.getElementById('txtInput').value; }</script><input type="text" id="txtInput" /><input type="button" value="OpenChild" onclick="OpenChildWindow()" />

通过判断子窗体的引用是否为空,我们还可以控制使其只能打开一个子窗体:


<script type="text/javascript">var childfunction OpenChildWindow(){    if(!child)            child = window.open('ChildPage.htm');       child.document.getElementById('txtInput').value            =document.getElementById('txtInput').value; }</script><input type="text" id="txtInput" /><input type="button" value="OpenChild" onclick="OpenChildWindow()" />

光这样还不够,当关闭子窗体时还必须对父窗体的child变量进行清空,否则打开子窗体后再关闭就无法再重新打开了:


<body onunload="Unload()"><script type="text/javascript">function SetValue(){    window.opener.document.getElementById('txtInput').value        =document.getElementById('txtInput').value;    window.close();}function Unload(){    window.opener.child=null;}</script><input type="text" id="txtInput" /><input type="button" value="SetFather" onclick="SetValue()" /></body>

Tags: ,

技术生涯

Powered by BlogEngine.NET 1.6.1.9  登录
Original Design by Laptop Geek, Adapted by onesoft