[英]How do you show/hide a div with Javascript?
我正在尝试使用javascript显示/隐藏div的内容,具体取决于选中的单选按钮。 我有一个onchange函数,可用于将div内容从一个div更改为另一个div,但是它不起作用。 如果您可以在jquery中执行此操作,那没关系,但是我对jquery不那么熟悉,所以如果您可以用它更新我的jsfiddle,我会很感激的:)这是JSFiddle: https ://jsfiddle.net/markusbond/au77Ladg/任何感谢您的帮助:)
JAVASCRIPT:
function changeSelection() {
if (document.getElementById("selectionTables").checked) {
document.getElementById("populateCheckBoxes").show;
document.getElementById("unpopulateCheckBoxes").hidden;
} else {
document.getElementById("unpopulateCheckBoxes").show;
document.getElementById("populateCheckBoxes").hidden;
}
}
HTML:
<form role="form">
<div class="row">
<!--this is the onchange call-->
<div class="radio col-xs-2" id="populateSelection" onchange="changeSelection()">
<!--this is the first radio button-->
<label>
<input type="radio" name="optradio" id="selectionTables" />Use Tables
</label>
<!--this is the second radio button-->
<label>
<input type="radio" name="optradio" id="selectionViews" />Use Views
</label>
</div>
<!--this is the first changed div-->
</div>
<div class="row" id="populateCheckBoxes">
<div class="checkbox">
<label>
<input type="checkbox" id="selectionCondition" />Condition
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" id="selectionDistribution" />Distribution
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" id="selectionProgram" />Program
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" id="selectionTreatment" />Treatment
</label>
</div>
</div>
<!--this is the second changed div-->
<div class="row" id="unpopulateCheckBoxes"></div>
</form>
使用JQuery FIDDLE的示例
的JavaScript
$('input:radio[name=optradio]').change(
function(){
if (this.value == 'selectionTables') {
$("#unpopulateCheckBoxes" ).hide();
$("#populateCheckBoxes").show();
}
else {
$("#unpopulateCheckBoxes" ).show();
$("#populateCheckBoxes").hide();
}
}
);
给您的第一个单选按钮一个值value="selectionTables"
<!--this is the first radio button-->
<label>
<input type="radio" name="optradio" id="selectionTables" value="selectionTables" />Use Tables
</label>
我希望我能正确理解你。
document.getElementById('myElementID').style.display = 'none'; // Will hide an element
document.getElementById('myElementID').style.display = 'block'; // Will show an element
值得注意的是,您将不会总是希望元素被display:block;
。 显示选项很多,您可能想根据元素的原始显示方式来回切换。
或通过jQuery,多数民众赞成在我更喜欢:
function changeSelection() {
if ($("#selectionTables").attr("checked") {
$("#populateCheckBoxes").show();
$("#unpopulateCheckBoxes").hide();
} else {
$("#unpopulateCheckBoxes").show();
$("#populateCheckBoxes").hide();
}
}
这是正确的方法
<script>
function changeSelection() {
if (document.getElementById("selectionTables").checked) {
document.getElementById("populateCheckBoxes").style.visibility = "hidden";
document.getElementById("unpopulateCheckBoxes").style.visibility = "visible";
} else {
document.getElementById("unpopulateCheckBoxes").style.visibility ="hidden";
document.getElementById("populateCheckBoxes").style.visibility = "visible";
}
}
</script>
您正在使用
document.getElementById("unpopulateCheckBoxes").hidden;
但是你应该
document.getElementById("populateCheckBoxes").style.visibility = "hidden";
希望这可以帮助
问题是html不支持show/hide
属性。 尝试将style
属性与display
属性一起使用。 也可以使用checked
属性进行默认的单选。
<input type="radio" name="optradio" id="selectionTables" checked='' />
function changeSelection() { var pop = "none", upop = "block"; if (document.getElementById("selectionTables").checked) { pop = "block"; upop = "none"; } document.getElementById("unpopulateCheckBoxes").style.display = upop; document.getElementById("populateCheckBoxes").style.display = pop; }
<form role="form"> <div class="row"> <!--this is the onchange call--> <div class="radio col-xs-2" id="populateSelection" onchange="changeSelection()"> <!--this is the first radio button--> <label> <input type="radio" name="optradio" id="selectionTables" checked='' />Use Tables </label> <!--this is the second radio button--> <label> <input type="radio" name="optradio" id="selectionViews" />Use Views </label> </div> <!--this is the first changed div--> </div> <div class="row" id="populateCheckBoxes"> <div class="checkbox"> <label> <input type="checkbox" id="selectionCondition" />Condition </label> </div> <div class="checkbox"> <label> <input type="checkbox" id="selectionDistribution" />Distribution </label> </div> <div class="checkbox"> <label> <input type="checkbox" id="selectionProgram" />Program </label> </div> <div class="checkbox"> <label> <input type="checkbox" id="selectionTreatment" />Treatment </label> </div> </div> <!--this is the second changed div--> <div class="row" id="unpopulateCheckBoxes"></div> </form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.