日期:2014-05-16 浏览次数:20472 次
<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>
------解决方案--------------------
<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>