Rails 3.我有一个div id,我试图根据选中的复选框的值隐藏或显示。我的脚本现在无法通过id获取元素。它返回null。我试过输入id和名称。都返回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。我尝试了#监控硬件和#监控硬件,所有这些都是空的。我怎么得到元素?谢谢(请不要恨我的李造型:)
溶液
function checkHardware() {
if ($('#device input:checkbox:eq(0)').is(':checked') || $('#device input:checkbox:eq(1)').is(':checked'))
{
$('#phonepbx').css('display', 'block');
}
等等。
发布于 2013-12-12 15:40:32
试着不使用“#”:
document.getElementById('survey_hardware_');
或者直接使用jQuery:
$('#survey_hardware_')
发布于 2013-12-12 15:42:30
你为什么要使用getElementById呢?您有jquery,所以请使用:
$("#survey_hardware_").find(
发布于 2013-12-12 15:47:39
类似于:(请注意新的和独特的id)
<li style="display:block;"><input id="phones" name="survey[hardware][]" type="checkbox" value="IP Phones" /> IP Phones</li>
<li style="display:block;"><input id="pbx" name="survey[hardware][]" type="checkbox" value="IP PBX Systems" /> IP PBX Systems </li>
<li style="display:block;"><input id="security" name="survey[hardware][]" type="checkbox" value="IP Security/Surveillance Systems" /> IP Security/Surveillance Systems</li>
<li style="display:block;"><input id="infra" name="survey[hardware][]" type="checkbox" value="IP infrastructure (cabling, switches...etc.)" /> IP infrastructure (cabling, switches...etc.)</li>
然后直接使用jQuery :已经建议的$('#new_id_here')
。
https://stackoverflow.com/questions/20547728
复制相似问题