您的位置:首页 > Web前端

【前端小代码】选中checkbox多选框,将值填入select中option中

2015-12-28 17:14 411 查看
通过js实现

代码如下

<script>
function checkbox()
{
var str=document.getElementsByName("box");
var shosetype=document.getElementById("select");
var objarray=str.length;
var chestr="";
var option = '';
for (i=0;i<objarray;i++)
{
if(str[i].checked == true)
{
chestr+=str[i].value+",";
option += '<option value="' + i + '">' + str[i].value + '</option>';
}
}
shosetype.innerHTML=option;
if(chestr == "")
{
alert("请先选择一个爱好~!");
}
else
{
alert("您先择的是:"+chestr);
}
}
</script>


html代码

选择您的爱好:
<input type="checkbox" name="box" id="box1" value="跳水" />跳水
<input type="checkbox" name="box" id="box2" value="跑步" />跑步
<input type="checkbox" name="box" id="box3" value="听音乐" />听音乐
<input type="button" name="button" id="button" onclick="checkbox()" value="提交" />
<select value="d" id="select">

</select>


效果图





内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: