2013-12-12 56 views
0

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'); 
} 

等等

+1

你可以有多个同名的ID吗? – tommyd456

+1

你不应该有多个具有相同id属性的元素。这在通过JS定位元素时会引起问题,并且与标准相违背。 – dSquared

+0

Ya ID在文档上下文中必须是唯一的,您的HTML无效 –

回答

2

尝试不以 '#':

document.getElementById('survey_hardware_'); 

或者直接使用jQuery:

$('#survey_hardware_') 
+0

什么是简单的修复! :D – DDDD

1

为什么你使用getElementById?你有jQuery的,所以用:

$("#survey_hardware_").find(
+0

好主意。我会用这个逻辑。 – DDDD

0

喜欢的东西:(注意新的和独特的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')如已经提出。

+0

问题在erb文件中。 HTML呈现如图所示。这就是RoR中的问题。

  • <%= check_box_tag'survey [hardware] []',“IP Phones”%> IP Phones
  • DDDD

    +1

    对不起,我忽略了那一点 - 你见过这个问题吗? http://stackoverflow.com/questions/992094/how-do-i-set-a-unique-id-for-checkboxes-in-a-multi-record-rails-form – tommyd456

    +0

    我听说过这个,这是一个好方法。谢谢 – DDDD