2012-08-17 30 views
0

我有一段时间JQuery代码,但我今天已经使用了一段时间,但今天我遇到了一个障碍 - 这个障碍是以数组输入的形式出现的。隐藏id使用jquery,但输入是一个数组

<div id="primary"> //oopse this one contains the survey info 
<input type="radio" name="quest[]" value="yes"> //was typing fast didnt realize i typed checkbox meant radio 
<input type="radio" name="quest[]" value="no"> 
</div> 
<div id="idnum"> // this one shows only if yes is selected 
blah blah blah 
</div> 

所以输入发送查询[] 我顺理成章的事情说,不使用数组 - 然而,这是不是一种选择。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 
<script> 

    $(document).ready(function() { 
    $("#quest[] selected").change(function (e) { 
     if ($(this).val() == "Yes") { //if query[] = Yes then run following 
     $("#idnum").hide();   //hide div id=idnum 
     } 
     else {  //if it no longer shows yes then run following 
     $("#idnum").show(); //show div id=idnum 
      }  
    }); 
    }); 

[]数组有这个阵列对于其他问题下的其他值也 我怎么解决这个问题集中在阵列中的一个特定的输入 - 每个div有不同的ID和我有找的能力数组[数字]如果需要的话。

对不起固定的一些事情并注明有点

我不知道这是否是可能的,但有没有可能与在特定

+5

'quest []'不是ID属性的值,对吧? – undefined 2012-08-17 17:19:26

+0

no quest []是单选按钮 – 2012-08-17 18:21:32

回答

0

jsFiddle DEMO

// to grab them by name starting with "quest[" the rest will be dynamic you said 
$('[name^="quest["]').on('change', function() { 
    if ($(this).attr('value') === 'yes') { 
     $(this).closest('div').hide(); 
    } 
}); 

如果我理解正确的问题,而你试图隐藏最近的父<div>和隐藏它,如果他们选择是。

0

使用#是上市的“是”价值发现为ID的任务[]的名称是

为CSS选择将

$('[name="quest[]"]') 

不过我不能确定你正在尝试做

查找检查任务[]/s的将是: -

$('[name="quest[]"]').find(':checked') 
1

我真的不明白你想要做什么,但它听起来像你只需要像:

$("[name^=quest][value=yes]").change(function() { 
    $("#idnum").toggle(); 
}); 

..但我有很多问题。你为什么让他们同时检查“是”和“否”?

+1

的输入值,这是一个意外 - 我的意思是收音机 - 我已经注释并修复了错误 – 2012-08-17 18:21:04