<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<span id="picking">
City:
<select name="city" id="city">
<option value="130100">石家庄</option>
<option value="130300">保定</option>
<option value="130500">张家口</option>
</select>
<span id="add" style="display: none;">
<input type="text" id="addCity" />
格式: 130100,石家庄
</span>
</span>
</body>
<script type="text/javascript">
document.getElementById("picking").ondblclick = function(){
document.getElementById("city").style.display = "none";
document.getElementById("add").style.display = "inline";
}
document.getElementById("addCity").onkeyup = function(e){
if(e.keyCode == 13) {
var city = document.getElementById("city");
var arr = this.value.split(",")
try{
var content = eval('({"dm":"' + arr[0] + '", "mc":"' + arr[1] + '"})');
if(content.mc === "undefined") {
throw("格式不正确!");
}
city.innerHTML += '<option value="' + content.dm + '">' + content.mc + '</option>';
}catch(e){
alert(e);
}
document.getElementById("city").style.display = "inline";
document.getElementById("add").style.display = "none";
document.getElementById("addCity").value = "";
}
}
</script>
</html>
Select 读写操作
最新推荐文章于 2023-09-27 00:15:00 发布