js添加删除select 非form形式

<html>     
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<TITLE>网页特效|Linkweb.cn/Js|---添加与删除Option对象</TITLE>
<script language="JavaScript">
// 添加选项
function wifi_white_mac_add(pos){
    var objSelect = document.getElementById("sel_white_mac");
    // 取得字段值
    var strName = document.getElementById("wifi_white_mac").value;
    var strValue = document.getElementById("wifi_white_mac").value;
    // 建立Option对象
    var objOption = new Option(strName,strValue);
    if (pos == -1 & pos > objSelect.options.length)
       objSelect.options[objSelect.options.length] = objOption;
    else
       objSelect.add(objOption, pos);
}
// 删除选项
function wifi_white_mac_del(){
    var objSelect = document.getElementById("sel_white_mac");
    objSelect.options[objSelect.selectedIndex] = null;
}

</script>
</HEAD>
<BODY>

<h2>添加与删除Option对象</h2>
<hr>

MAC地址 : <input type="text" id="wifi_white_mac" name="wifi_white_mac" value="">
</br>
<select id="sel_white_mac" name="sel_white_mac"  size="10">
<option value="中国" Selected>中国</option>
<option value="日本">日本</option>
<option value="美国">美国</option>
</select>
</br>
<input type="button" οnclick="wifi_white_mac_add(0)" value="插入">
<input type="button" οnclick="wifi_white_mac_del()" value="删除">

</BODY>

</HTML>

=============网上其他形式1===========================

<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title>javascript select options text value</title>
<meta name="keywords" content="javascript select options text value add modify delete set">
<meta name="description" content="javascript select options text value add modify delete set">
<script language="javascript">
<!--
function watch_ini(){ // 初始
for(var i=0; i<arguments.length; i++){
var oOption=new Option(arguments[i],arguments[i]);
document.getElementById("MySelect")[i]=oOption;
}
}
function watch_add(f){ // 增加
var oOption=new Option(f.word.value,f.word.value);
f.keywords[f.keywords.length]=oOption;
}
function watch_sel(f){ // 编辑
f.word.value = f.keywords[f.keywords.selectedIndex].text;
}
function watch_mod(f){ // 修改
f.keywords[f.keywords.selectedIndex].text = f.word.value;
}
function watch_del(f){ // 删除
f.keywords.remove(f.keywords.selectedIndex);
}
function watch_set(f){ // 保存
var set = "";
for(var i=0; i<f.keywords.length; i++){
set += f.keywords[i].text + ";";
}
confirm(set);
}
//-->
</script>
</head>
<body>
<form name="watch" method="post" action="">
<select id="MySelect" name="keywords" size="10" οnchange="watch_sel(this.form)"></select><br>
<script language="javascript">
<!--
watch_ini("我","你","妳","他","她","它","尔"); // 初始关键词
//-->
</script>
<input type="text" name="word" /><br />
<input type="button" value="增加" οnclick="watch_add(this.form);" />
<input type="button" value="修改" οnclick="watch_mod(this.form);" />
<input type="button" value="删除" οnclick="watch_del(this.form);" />
<input type="button" value="保存" οnclick="watch_set(this.form);" />
</form>

===========网上其他形式2=================

<html>     
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<TITLE>网页特效|Linkweb.cn/Js|---添加与删除Option对象</TITLE>
<script language="JavaScript">
// 添加选项
function addOption(pos){
    var objSelect = document.myForm.mySelect;
    // 取得字段值
    var strName = document.myForm.myOptionName.value;
    var strValue = document.myForm.myOptionValue.value;
    // 建立Option对象
    var objOption = new Option(strName,strValue);
    if (pos == -1 & pos > objSelect.options.length)
       objSelect.options[objSelect.options.length] = objOption;
    else
       objSelect.add(objOption, pos);
}
// 删除选项
function deleteOption(type){
    var objSelect = document.myForm.mySelect;
    if (type == true)
       objSelect.options[objSelect.selectedIndex] = null;
    else
       objSelect.remove(objSelect.selectedIndex);
}
// 显示选项信息
function showOption(objForm){
    var objSelect = objForm.mySelect;
    var name = objSelect.options[objSelect.selectedIndex].text;
    var value = objSelect.options[objSelect.selectedIndex].value;
    alert(name + " = " + value);
}
</script>
</HEAD>
<BODY>

<h2>添加与删除Option对象</h2>
<hr>
<form name="myForm">
<select name="mySelect">
<option value="中国" Selected>中国</option>
<option value="日本">日本</option>
<option value="美国">美国</option>
</select>
<input type="button" οnclick="showOption(this.form)" value="显示">
<input type="button" οnclick="deleteOption(true)" value="删除">
<input type="button" οnclick="deleteOption(false)" value="Remove方法"><br><br>
选项名称 : <input type="text" name="myOptionName" value="英国"><br>
选项的值 : <input type="text" name="myOptionValue" value="value4">
<input type="button" οnclick="addOption(-1)" value="添加">
<input type="button" οnclick="addOption(0)" value="插入">
</form>
</BODY>
</HTML>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值