JS向下拉框添加内容

1.在一个页面上添加

add

<html>
 
 <head>dialog</head>
 <script>
 function addOption(){
  var item = document.getElementById("item").value;
  var itemV = document.getElementById("itemV").value;
  var list = document.getElementById("list");
  list.options.length = list.options.length+1;
  var option = new Option(item,itemV,false,true);
  list.options[list.options.length-1]= option;
  alert("ok");
 } 
 </script>
<body>
 <select name="list" id="list"
      style="width:240px" size="5">
    </select>
   item:<input type="text" name="item" id="item"/>
   value:<input type="text" name="itemV" id="itemV"/>
  <input type="button" name="add" id="id" value="add" onClick="addOption();"/>
</body>
</html>

 

2.通过子窗口向父窗口 添加

parent.html:

<html>
 <head>add opt</head>
 <script>
 
 function openWin()

 window.open("dialog.html",'','width=500px, height=360px,left='+(screen.width/2-300)+'px,top='+(screen.height/2-175)+'px,unadorned=off,scrollbars=yes,resizable=yes,status=no');

 </script>
 <body>
  <select name="condition" id="condition"
      style="width:240px" size="5">
    </select>
   item:<input type="text" name="item" id="item"/>
   value:<input type="text" name="itemV" id="itemV"/>
  <input type="button" name="add" id="id" value="add" onClick="openWin();"/>
 </body>
</html>

children.html

<html>
 
 <head>dialog</head>
 <script>
 function addOption(){
  var item = document.getElementById("item").value;
  var itemV = document.getElementById("itemV").value;
  var condition = window.opener.document.getElementById("condition");
   var newOpt = window.opener.document.createElement("option");//需要调用父窗口的方法来创建option对象,因为只允许窗体访问自己的变量对象。
  newOpt.text=item;
  newOpt.value=itemV;
  condition.options.add(newOpt);
  alert("ok");
 } 
 </script>
<body>
 item:<input type="text" name="item" id="item"/>
   value:<input type="text" name="itemV" id="itemV"/>
  <input type="button" name="add" id="id" value="add" onClick="addOption();"/>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值