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無效 –