Ruby on Rails 3. 我有一个div id我试图隐藏或显示基于选中复选框的值。我的脚本现在无法通过ID获取元素。它返回null。我尝试了输入ID和名称。两者都返回null。JQuery返回元素为null
下面是脚本:
$(document).ready(function() {
$('#device').change(function(event){
var hardware = document.getElementById('#survey_hardware_');
if (($(hardware).find("input[value='IP Phones']:checked").length) || ($(hardware).find("input[value='IP PBX Systems']:checked").length))
{
$('#phonepbx').css('display', 'inline');
}
else
{
$('#phonepbx').css('display', 'none');
}
});
});
这是渲染HTML问题:
<div class="row" id="device">
<ul>1. What IP hardware does your company frequently sell and/or install? (select all that apply)<br/>
<li style="display:block;"><input id="survey_hardware_" name="survey[hardware][]" type="checkbox" value="IP Phones" /> IP Phones</li>
<li style="display:block;"><input id="survey_hardware_" name="survey[hardware][]" type="checkbox" value="IP PBX Systems" /> IP PBX Systems </li>
<li style="display:block;"><input id="survey_hardware_" name="survey[hardware][]" type="checkbox" value="IP Security/Surveillance Systems" /> IP Security/Surveillance Systems</li>
<li style="display:block;"><input id="survey_hardware_" name="survey[hardware][]" type="checkbox" value="IP infrastructure (cabling, switches...etc.)" /> IP infrastructure (cabling, switches...etc.)</li>
</ul>
</div>
摘要:
document.getElementById('#survey_hardware_');
返回null。我试过#survey [硬件] []和#survey [硬件] [0],全部为空。我如何获得元素?谢谢(请不要恨我李造型:)
解决方案
function checkHardware() {
if ($('#device input:checkbox:eq(0)').is(':checked') || $('#device input:checkbox:eq(1)').is(':checked'))
{
$('#phonepbx').css('display', 'block');
}
等等
你可以有多个同名的ID吗? – tommyd456
你不应该有多个具有相同id属性的元素。这在通过JS定位元素时会引起问题,并且与标准相违背。 – dSquared
Ya ID在文档上下文中必须是唯一的,您的HTML无效 –