简体   繁体   English

显示/隐藏div输入广播

[英]Show/hide div input radio

I need to show/hide a div when a specific value is selected but I think the JavaScript selector 'or' doesn't not working. 选择特定值时,我需要显示/隐藏div,但我认为JavaScript选择器“或”不起作用。 But its working when there is only one condition. 但是,只有一种情况时,它才能工作。 I checked the documentation but nothing helped me 我检查了文档,但没有任何帮助

<div class="panel-body">
    <div class="table-responsive">
        <table class="table table-bordered" style="width: auto !important;">
            <tbody class="table-striped">
                <tr>
                    <td rowspan="4" style="vertical-align: middle;">Le salarié a-t-il un projet professionnel ?</td>
                    <td>
                        <label class="radio-inline">
                            <input type="radio" name="choixPP" id="choixPP" value="LE"> à Limousin Expansion
                        </label>
                    </td>
                </tr>
                <tr>
                    <td>
                        <label class="radio-inline">
                            <input type="radio" name="choixPP" id="choixPP" value="CRL"> au Conseil Régional du Limousin
                        </label>
                    </td>
                </tr>
                <tr>
                    <td>
                        <label class="radio-inline">
                            <input type="radio" name="choixPP" id="choixPP" value="EXT"> a l'extérieur
                        </label>
                    </td>
                </tr>
                <tr>
                    <td>
                        <label class="radio-inline">
                            <input type="radio" name="choixPP" id="choixPP" value="0" checked> pas de projet professionnel
                        </label>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
    <div class="form-group" id="divChoixPP" style="display:none;">
        <label for="divChoixPP" class="col-sm-2 control-label">Si oui, préciser le projet</label>
        <div class="col-sm-6">
            <textarea class="form-control" name="divChoixPP" id="divChoixPP" rows="6" cols="20"></textarea>
        </div>
        <!-- .col-sm-6 -->
    </div>
    <!-- .form-group -->
</div>

JavaScript: JavaScript:

$(document).ready(function () {
    $("#divChoixPP").hide();

    $("input:radio[name='choixPP']").change(function () {

        if (this.value == 'LE' || 'CRL' || 'EXT' && this.checked) {
            $("#divChoixPP").show();
        } else {
            $("#divChoixPP").hide();
        }
    });
});
if(this.value == 'LE' || 'CRL' || 'EXT')

is not doing what you think it is doing. 没有按照您认为的去做。 This is just checking the value equals to LE . 这只是检查等于LE的值。 The OR operator is short-circuit to get the first truthy value. OR运算符短路以获取第一个真实值。 As any non-empty string is truthy, this.value == 'LE' || 'CRL' || 'EXT' 由于任何非空字符串都是真实的,因此this.value == 'LE' || 'CRL' || 'EXT' this.value == 'LE' || 'CRL' || 'EXT' this.value == 'LE' || 'CRL' || 'EXT' expression is evaluated to this.value == 'LE' this.value == 'LE' || 'CRL' || 'EXT'表达式计算为this.value == 'LE'

Use 采用

if(this.value === 'LE' || this.value === 'CRL' || this.value ==='EXT'

Or create an array and check if the element is present in the array. 或创建一个数组并检查元素是否存在于数组中。

var values = ['LE', 'CRL', 'EXT'];

if (values.indexOf(this.value) !== -1)

 $(document).ready(function() { var values = ['LE', 'CRL', 'EXT']; $("input:radio[name='choixPP']").change(function() { $('#divChoixPP').toggle(this.checked && values.indexOf(this.value) !== -1) }); }); 
 #divChoixPP { display: block; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <div class="panel-body"> <div class="table-responsive"> <table class="table table-bordered" style="width: auto !important;"> <tbody class="table-striped"> <tr> <td rowspan="4" style="vertical-align: middle;">Le salarié at-il un projet professionnel ?</td> <td> <label class="radio-inline"> <input type="radio" name="choixPP" id="choixPP" value="LE">à Limousin Expansion </label> </td> </tr> <tr> <td> <label class="radio-inline"> <input type="radio" name="choixPP" id="choixPP" value="CRL">au Conseil Régional du Limousin </label> </td> </tr> <tr> <td> <label class="radio-inline"> <input type="radio" name="choixPP" id="choixPP" value="EXT">a l'extérieur </label> </td> </tr> <tr> <td> <label class="radio-inline"> <input type="radio" name="choixPP" id="choixPP" value="0" checked>pas de projet professionnel </label> </td> </tr> </tbody> </table> </div> <div class="form-group" id="divChoixPP" style="display:none;"> <label for="divChoixPP" class="col-sm-2 control-label">Si oui, préciser le projet</label> <div class="col-sm-6"> <textarea class="form-control" name="divChoixPP" id="divChoixPP" rows="6" cols="20"></textarea> </div> <!-- .col-sm-6 --> </div> <!-- .form-group --> </div> 

Your usages of || ||用法 is wrong. 是错的。 Your if condition should be like following. 您的if条件应如下所示。

if(this.value == 'LE' || this.value=='CRL' || this.value=='EXT' && this.checked){
    //do your task here
}

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

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