jquery - 这个脚本有什么问题?
问题描述
https://jsfiddle.net/Ld5s4jLw/1/
我不知道这有什么问题,它不起作用,我尝试了很多东西但无法使它起作用......
function toggleAutre(bloc) {
if ($('#' + bloc + '_oui').is(':checked')) {
$('#' + bloc + '_texte').show('slow');
alert('Oui');
} else if ($('#' + bloc + '_non').is(':checked')) {
$('#' + bloc + '_texte').hide('slow');
alert('Non');
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col">
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="nbre_poste_inoccupe" id="nbre_poste_inoccupe_oui" value="1" onclick="toggleAutre('nbre_poste_inoccupe');" />
<label class="form-check-label" for="nbre_poste_inoccupe_oui">OUI</label>
</div>
<div class="form-group" id="nbre_poste_inoccupe_text_div" style="display: none;">
<label for="nbre_poste_inoccupe_text" style="font-size: 0.7em; font-weight: 400;">Si OUI, combien ?</label>
<input class="form-control" type="number" name="nbre_poste_inoccupe" id="nbre_poste_inoccupe_text" style="width: 5em;" value="1" onclick="toggleAutre('nbre_poste_inoccupe');" />
</div>
</div>
<div class="col">
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="nbre_poste_inoccupe" id="nbre_poste_inoccupe_non" value="0">
<label class="form-check-label" for="nbre_poste_inoccupe_non">NON</label>
</div>
</div>
解决方案
如果我对您的理解正确,id
那么您要显示的动态不正确。您正在使用选择器(经过计算,对吗?)#nbre_poste_inoccupe_texte
来查找 div nbre_poste_inoccupe_text_div
。只需修复它,它就会为您工作。
function toggleAutre(bloc) {
if ($('#' + bloc + '_oui').is(':checked')) {
console.log('#' + bloc + '_texte_div');
$('#' + bloc + '_text_div').show('slow');
alert('Oui');
} else if ($('#' + bloc + '_non').is(':checked')) {
$('#' + bloc + '_texte').hide('slow');
alert('Non');
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col">
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="nbre_poste_inoccupe" id="nbre_poste_inoccupe_oui" value="1" onclick="toggleAutre('nbre_poste_inoccupe');" />
<label class="form-check-label" for="nbre_poste_inoccupe_oui">OUI</label>
</div>
<div class="form-group" id="nbre_poste_inoccupe_text_div" style="display: none;">
<label for="nbre_poste_inoccupe_text" style="font-size: 0.7em; font-weight: 400;">Si OUI, combien ?</label>
<input class="form-control" type="number" name="nbre_poste_inoccupe" id="nbre_poste_inoccupe_text" style="width: 5em;" value="1" onclick="toggleAutre('nbre_poste_inoccupe');" />
</div>
</div>
<div class="col">
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="nbre_poste_inoccupe" id="nbre_poste_inoccupe_non" value="0">
<label class="form-check-label" for="nbre_poste_inoccupe_non">NON</label>
</div>
</div>
推荐阅读
- css - 对于带边框的内部 DIV,如何滚动 DIV 的内容,但保持边框固定?
- sql - 绑定动态查询以填充 GridView 或类似的 asp.net webforms 控件以在网页中显示结果
- azure - Azure Metrics Graphs 根据样式显示不同的结果
- java - 来自同一主机上的应用程序的请求导致问题?
- javascript - 如何查看表示应用悬停的条的当前元素
- reactjs - 每小时刷新令牌的最佳方法?
- python - 使用交叉验证(scikit-learn)时获取单个数据点的错误
- vue.js - 如何根据两个数组的比较进行过滤?
- perl - 如何在 Getopt::Long getoptions 中打印命令参数
- python - AttributeError:类型对象“pandana.cyaccess.cyaccess”没有属性“__reduce_cython__”