我在尝试使用javascript获取复选框的文本时遇到了一些困难。这里是我的html代码:在JavaScript中获取选中的复选框的文本
content += "<tr><td><input id='type_select1' class=\"pss\" type=\"checkbox\" onclick='queryPSS()' >Commercial and Residential</td><td><input id='type_select2' class=\"pss\" type=\"checkbox\" onclick='queryPSS()' >Commercial</td></tr>";
content += "<tr><td><input id='type_select3' class=\"pss\" type=\"checkbox\" onclick='queryPSS()'>Heavy Vehicle Park</td><td><input id='type_select4' class=\"pss\" type=\"checkbox\" onclick='queryPSS()'>Hospital</td></tr>";
content += "<tr><td><input id='type_select5' class=\"pss\" type=\"checkbox\" onclick='queryPSS()'>Hotel</td><td><input id='type_select6' class=\"pss\" type=\"checkbox\" onclick='queryPSS()'>Industrial</td></tr>";
content += "<tr><td><input id='type_select7' class=\"pss\" type=\"checkbox\" onclick='queryPSS()'>Industrial-White</td><td><input id='type_select8' class=\"pss\" type=\"checkbox\" onclick='queryPSS()'>Office</td></tr>";
content += "<tr><td><input id='type_select9' class=\"pss\" type=\"checkbox\" onclick='queryPSS()'>Recreation</td><td><input id='type_select10' class=\"pss\" type=\"checkbox\" onclick='queryPSS()'>Residential (Landed)</td></tr>";
content += "<tr><td><input id='type_select11' class=\"pss\" type=\"checkbox\" onclick='queryPSS()'>Residential (Non-Landed)</td><td><input id='type_select12' class=\"pss\" type=\"checkbox\" onclick='queryPSS()'>Residential</td></tr>";
content += "<tr><td><input id='type_select13' class=\"pss\" type=\"checkbox\" onclick='queryPSS()'>White Site</td></tr>";
这里是我如何才能选中的复选框的文本:
var type_select = document.getElementById('type_select');
if(type_select.checked == true)
{
var type_filter = type_select.text;
alert(type_filter);
}
然而,当我检查的检查框,弹出消息显示“undefined
”。我不知道出了什么问题。提前致谢。
我希望你认识到多个表单元素不能有相同的ID –
有没有其他方法可以解决这个问题?因为我想要做的是获取复选框的文本并执行其他功能 –
你可以给他们diffids.for例如。 type_select1,type_select2,type_select3..point要注意的是,它们必须是唯一的。即它 –