如何设置和获取JavaScript函数的check_box_tag的ID? [英] How do you set and get the ID of check_box_tag for javascript function?
问题描述
我有一个带check_box_tags的Ruby on Rails 3表单.如何设置ID,然后在javascript函数中获取值? 这是check_box_tag
I have a Ruby on Rails 3 form with check_box_tags. How do you set an ID and then get the value in a javascript function? Here is the check_box_tag
<div class="row" id="device">
<ul>
<li><%= check_box_tag 'survey[hardware][]', "IP Phones" %> IP Phones</li>
</ul>
</div>
我的JavaScript
My javascript
$(document).ready(function() {
$('#device').change(function(){
if (($('#device').val() == "IP Phone") || ($('#device').val() == "IP PBX Systems"))
{
$('#phonepbx').css('display', 'none');
}
else
{
$('#phonepbx').css('display', 'inline');
}
});
});
我不知道如何设置和获取check_box_tag的ID.我试图根据是否选择div来显示或隐藏div id ="phonepbx".如果他们选择了前两个复选框之一,则我想显示#phonepbx div.否则将其隐藏.
I do not know how to set and get the id of the check_box_tag. I am trying to show or hide the div id="phonepbx" based on whether what they choose. If they select one of the first two check boxes I want to show the #phonepbx div. Otherwise keep it hidden.
谢谢
编辑 HTML的最终结果是:
EDIT This is what the HTML ends up as:
<li><input id="survey_hardware_" name="survey[hardware][]" type="checkbox" value="IP Phones" /> IP Phones</li>
我确实需要jquery和jquery_ujs. 我还需要其他东西吗?
I do have require jquery and require jquery_ujs. Do I need something else?
解决方案
function checkHardware() {
if ($('#device input:checkbox:eq(0)').is(':checked') || $('#device input:checkbox:eq(1)').is(':checked'))
{
$('#phonepbx').css('display', 'block');
}
推荐答案
根据您的指示,您只是在检查是否已选中两个特定复选框中的至少一个:
Per your instructions, you are just checking to see if at least one of two specific checkboxes is checked:
$(document).ready(function() {
$('#device').change(function(event){
if ($(event.target).filter("input[value='IP Phone']:checked").length ||
$(event.target).filter("input[value='IP PBX Systems']:checked").length)
{
$('#phonepbx').css('display', 'inline');
}
else
{
$('#phonepbx').css('display', 'none');
}
});
});
这篇关于如何设置和获取JavaScript函数的check_box_tag的ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!