简体   繁体   English

jQuery:选择第一个或第二个单选按钮时显示/隐藏Div

[英]jQuery: show / hide Div when first or second radio button is selected

I am new to jQuery and hope someone here can help me with this. 我是jQuery的新手,希望这里的人可以帮助我。

I am trying to show / hide a div ( 'requestDetails' ) if the first OR second of a variable range of radio buttons is selected (the reason for this is that I would like to avoid hard-coding specific values to check for). 我正在尝试显示/隐藏div'requestDetails' ), 如果 选择 可变范围的单选按钮 的第一或第二 (其原因是我想避免对要检查的特定值进行硬编码)。

So far I only have a hard-coded solution but this doesn't work properly and I am not sure about how to address only the first and second radio button without this. 到目前为止,我只有一个硬编码的解决方案,但这不能正常工作,而且我不确定如何在没有此解决方案的情况下仅解决第一个和第二个单选按钮。

I tried to avoid using IDs or further classes on the radio buttons to reduce code but can add this if recommended. 我试图避免在单选按钮上使用ID或其他类来减少代码,但是如果建议可以添加此代码。

My jQuery: 我的jQuery:

$('[name=requestType]').on('change', function(){
    if( ($('[name=requestType]').val() == 'value1') || ($('[name=requestType]').val() == 'value2') ){
        $('#requestDetails').show();
    }else{
        $('#requestDetails').hide();
    }
});

My HTML: 我的HTML:

<input type="radio" name="requestType" value="value1" />Value1  
<input type="radio" name="requestType" value="value2" />Value2  
<input type="radio" name="requestType" value="value3" />Value3  
// ...
<div id="requestDetails" class="hidden"> ... </div>

Many thanks in advance for any help, Mike 迈克非常感谢您的任何帮助,迈克

try 尝试

$('[name=requestType]').on('change', function () {

    $('#requestDetails').toggle((this.value == 'value1' || this.value == 'value2'));

});

DEMO DEMO

If I were you, I would identify the buttons you would like to use as triggers for this show/hide event with a new class. 如果我是您,我将使用新的类来确定要用作此显示/隐藏事件的触发器的按钮。 Then attach the event to this class of button. 然后将事件附加到此类按钮。 This does not add much in code overhead. 这不会增加太多代码开销。

Example: 例:

<input type="radio" class="special" name="requestType" value="value1" />Value1  
<input type="radio" class="special" name="requestType" value="value2" />Value2  
<input type="radio" name="requestType" value="value3" />Value3  


$('.special').on('change', function(){
    if( ($('[name=requestType]').val() == 'value1') || ($('[name=requestType]').val() == 'value2') ){
        $('#requestDetails').show();
    }else{
        $('#requestDetails').hide();
    }
});

This will result in button 1 and button 2 affecting the div with id=requestDetails, while button 3 or any others without the class will not. 这将导致按钮1和按钮2使用id = requestDetails影响div,而按钮3或任何其他没有该类的按钮则不会。

With no hardcoding, use the parent containing the inputs and use :nth-child selector. 如果不进行硬编码,则使用包含输入的父项,并使用:nth-​​child选择器。 See jsfiddle for example : 参见jsfiddle例如:

$('[name=requestType]').on('change', function () {
    if($('#parent input:nth-child(2)').val() == $(this).val() ||$('#parent input:nth-child(3)').val() == $(this).val())
    $('#requestDetails').toggle();
});

http://jsfiddle.net/cwahL1tz/11/ http://jsfiddle.net/cwahL1tz/11/

Also made one with the exact behavior you wan to obtain : http://jsfiddle.net/cwahL1tz/13/ 还用您想要获得的确切行为制作了一个: http : //jsfiddle.net/cwahL1tz/13/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM