js获取checkbox复选框选中的选项实例
有关javascript获取checkbox复选框的实例数不胜数。
js实现:
varform=document.getElementById("form2"); varfield=form.elements["test2"]; varoption=Dining.getSelectedOption(form,field); varmessage=""; for(vari=0,len=option.length;i<len;i++){ message+="Selectid:"+option[i].id+"\nSelectedname:"+option[i].name+"\nSelectedvalue:"+option[i].value+"\n\n"; } alert(message); /*获取选中的选项*/ getSelectedOption:function(selectform,selectionfield){ varresult=[]; varoption=null; for(vari=0;i<selectionfield.length;i++){ option=selectionfield[i]; if(option.checked){ result.push(option); }www.nhooo.com } returnresult; } <formid='form2'> <label>排序:</label><inputid='aaaaa'type='checkbox'name='test2'value='1'> <labelfor='aaaaa'>月销量</label><inputid='bbbbb'type='checkbox'name='test2'value='2'> <labelfor='bbbbb'>评分</label><inputid='ccccc'type='checkbox'name='test2'value='3'> <labelfor='ccccc'>优惠</label><brstyle='clear:both'><label>分类:</label><inputid='ddddd'type='checkbox'name='test2'value='4'> <labelfor='ddddd'>商务套餐</label><inputid='eeeee'type='checkbox'name='test2'value='5'> <labelfor='eeeee'>凉菜</label><inputid='fffff'type='checkbox'name='test2'value='6'> </form>",