Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取JavaScript中选中复选框的文本_Javascript_Html_Checkbox - Fatal编程技术网

获取JavaScript中选中复选框的文本

获取JavaScript中选中复选框的文本,javascript,html,checkbox,Javascript,Html,Checkbox,我在尝试使用javascript获取复选框的文本时遇到了一些困难。以下是我的html的代码: content += "<tr><td><input id='type_select1' class=\"pss\" type=\"checkbox\" onclick='queryPSS()' >Commercial and Residential</td><td><input id='type_select2' class=\"ps

我在尝试使用javascript获取复选框的文本时遇到了一些困难。以下是我的html的代码:

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>";

但是,当我选中复选框时,弹出消息显示“
未定义”
”。我想知道出了什么问题。提前谢谢。

所以我终于明白了您的意图。首先,让我们从内容字符串中考虑一个示例。

<td><input class=\"pss\"  type=\"checkbox\" onclick='queryPSS()' value = "myValue">Commercial and Residential</td>
在下面的某个地方,你这样定义函数

function customFucntion(idValue){
   var elem = idValue.value;
}

就这样。这应该行得通。

我希望您认识到,多个表单元素不能具有相同的IDI。是否有其他方法解决此问题?因为我要做的是获取选中复选框的文本并执行其他功能您可以为它们提供不同的ID。例如,type_select1、type_select2、type_select3..需要注意的是它们必须是唯一的。这就是问题所在,但在我将它们设置为不同的ID之后,我如何将选中的复选框存储为type_filter?你能给我一些吗examples@user13500我已经更改了ID,但如何继续?不要为
ID
s操心-在大多数情况下,人们会花费大量时间找出不能给多个元素相同的ID,然后尝试找出如何给它们所有唯一的ID,你不需要它们。只需调用
customFunction(this)
,在函数中,
idValue.value
@Dennis,这是一个很好的观点。我会记住的。谢谢:)所以对于我的情况,在复选框标记中,我应该放这个:onclick=queryps(this)?是的。但也要确保包含该函数。elem变量将包含您需要的值。太好了。很高兴我能帮忙。
onclick = customFunction(this)
function customFucntion(idValue){
   var elem = idValue.value;
}