PHP初入--添加内容到框框里并删除

时间:2022-03-09 03:13:38

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style>
select{
width: 200px;
}
</style>
</head>
<body>
<select id="s1" size="7" multiple="multiple"></select>
<input type="text" id="txt" />
<input type="button" id="btn" value="添加" />
<input type="button" id="BTN_de1" value="删除" />
</body>
</html>
<script>

// 先进行定义
var s1 = document.getElementById("s1");
var txt = document.getElementById("txt");
var btn = document.getElementById("btn");
// 方法
btn.onclick = function(){
if(txt.value.trim() !=""){
var temp_opt = document.createElement("option");
temp_opt.innerText = txt.value;
s1.appendChild(temp_opt);
txt.value="";
}
}




var BTN_de1 = document.getElementById("BTN_de1");
BTN_de1.onclick = function(){
var x = s1.selectedOptions[0];
s1.removeChild(x);
}
</script>