正在选中复选框显示名称

getting checked checkbox display name

本文关键字:显示 复选框      更新时间:2023-09-26

我有一组复选框:

<input type="checkbox" value="111" name="checkbox"  id="1">india
<input type="checkbox" value="121" name="checkbox" id="21">pak
<input type="checkbox" value="131" name="checkbox" id="31">china
<!-- ... -->
<input type="checkbox" value="141" name="checkbox" id="10">srilanka

我想获得选中的复选框的显示名称,比如印度,巴基斯坦…我尝试了下面的代码,它给了我值111,121,....

    field= document.form.checkbox;
     for(var i=0; i < field.length; i++)
                       {  
                       if(document.getElementById(i).checked==true)
                         Cities+=document.classifiedForm.checkbox[i].value + ",";

                        } 

你可以在复选框中设置其他一些其他属性作为国家名称,我认为你可以设置为class

<input type="checkbox" value="111" class="India"  id="1">india
<input type="checkbox" value="121" class="Pak" id="21">pak

和javascript中的

field= document.form.checkbox;
 for(var i=0; i < field.length; i++)
     {  
       if(document.getElementById(i).checked==true)
               Cities+=document.classifiedForm.checkbox[i].getAttribute("class")+ ",";
    }