我是初学者,请教各位大师们
大师们好:我想要复选框中选中的value值显示到输入框中,改怎么弄,谢谢!
<form id="form" method="post">
<input type="text" value=" " name="originalprice"/><!--输入框-->
<input type="checkbox" value="news" name="box[]" />
<input type="checkbox" value="music" name="box[]" />
<input type="checkbox" value="read" name="box[]" />
<input type="checkbox" value="global" name="box[]" />
</form>
改怎么写代码,来控制,谢了!
[解决办法]
- HTML code
<form id="form" method="post"> <input type="text" value=" " name="originalprice" /> <input type="checkbox" value="news" name="box[]" /> <input type="checkbox" value="music" name="box[]" /> <input type="checkbox" value="read" name="box[]" /> <input type="checkbox" value="global" name="box[]" /></form><script type="text/javascript">var obj = document.getElementsByName('box[]');for (var i = 0; i < obj.length; i ++) obj[i].onclick = function() { var ar = []; for (var j = 0; j < obj.length; j ++) if (obj[j].checked) ar.push(obj[j].value); document.getElementsByName('originalprice')[0].value = ar.join('/');}</script>
[解决办法]
- HTML code
<form id="form" method="post"><input type="text" value=" " name="originalprice" id='ipt'/><!--输入框--><input type="checkbox" value="news" name="box[]" onclick='setValue(this)' /><input type="checkbox" value="music" name="box[]" onclick='setValue(this)'/><input type="checkbox" value="read" name="box[]" onclick='setValue(this)'/><input type="checkbox" value="global" name="box[]" onclick='setValue(this)'/></form><script type='text/javascript'>var result = {};function setValue(el){ var array = []; if(el.checked) result[el.value] = el.value; else delete result[el.value]; for(var key in result) array.push(key) document.getElementById('ipt').value = array.join('/')}</script>