2013-12-12 139 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