限制单选按钮显示,javascript
Restrict radio button display, javascript
我有一个下拉列表。当点击其中一个项目时,它应该显示单选按钮来显示。
我一直到这里。现在的问题是
我有四个单选按钮。我想显示2个选项并隐藏2个选项。在javascript中,
我该怎么做?
可以使用jquery
$(function() {
$('#dropdownId').change(function() { //when the dropdown change
var selectValue = $(this).val(); //Get it's value
if (selectValue == 1) {
$("#radioButtonId1").show();
$("#radioButtonId2").show();
$("#radioButtonId3").hide();
$("#radioButtonId4").hide();
}
});
}
你也可以在你的选项上使用数据来获得需要显示的选项:
<input type = "radio" class="DependOnSelect" data-showonid="1,3,5" />
<input type = "radio" class="DependOnSelect" data-showonid="2,4,6" />
<input type = "radio" class="DependOnSelect" data-showonid="1,4,6" />
<input type = "radio" class="DependOnSelect" data-showonid="2,3,5" />
$(function() {
$('#dropdownId').change(function() {
var selectValue = $(this).val();
if (selectValue == 1) {
$('.DependOnSelect').each(function() { //for each Options
var splited = $(this).data("showonid").split(',');
if (jQuery.inArray(selectValue, splited) > -1)
$(this).show(); // if the data-showonid contains the id, show it
else
$(this).hide();
});
}
});
}
JSFIDLE在jquery中禁用元素
$('#ElementId').attr("disabled", "disabled);
:
$('#ElementId').removeAttr("disabled");
Html:
<select id="dropdownId" onChange="jsFunction()">
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
<option value='4'>4</option>
</select>
<br/>
<input type="radio" name="option" id="option1" />1
<br/>
<input type="radio" name="option" id="option2" />2
<br/>
<input type="radio" name="option" id="option3" />3
<br/>
<input type="radio" name="option" id="option4" />4
<br/>
javascript: function jsFunction() {
var myselect = document.getElementById("dropdownId");
var value = myselect.options[myselect.selectedIndex].value;
if (value == 1) {
document.getElementById('option1').disabled = 'disabled';
} else {
document.getElementById('option1').disabled = '';
}
if (value == 2) {
document.getElementById('option2').disabled = 'disabled';
} else {
document.getElementById('option2').disabled = '';
}
if (value == 3) {
document.getElementById('option3').disabled = 'disabled';
} else {
document.getElementById('option3').disabled = '';
}
if (value == 4) {
document.getElementById('option4').disabled = 'disabled';
} else {
document.getElementById('option4').disabled = '';
}
}
JSFIDLE
嗨,你可以使用"disabled"来禁用任何特定的选项
<input type="radio" name="someName" value="someValue" id="someId" disabled>
Then get the two options you want to hide and make display = "none";
相关文章:
- PHP Javascript显示/隐藏按钮不工作
- 通过javascript显示和更改文本
- 是否可以用JavaScript显示等效的文件夹对话框
- 如何使用javascript显示图像
- BMI计算器以Javascript显示结果
- 单击“JavaScript显示变量”
- Javascript:显示带有复选框值的图像
- 通过ID JavaScript显示随机背景图像
- 无法在qualtrics上使用javascript显示背景图像
- 日期选择器无法使用javascript显示
- javascript 显示空白灰色层
- 页面加载后通过javascript显示图像
- 使用JavaScript显示鼠标的x和y位置
- 用javascript显示html表
- 使用 JavaScript 显示 Laravel's Variable
- javascript显示地理位置
- 搜索XML文件并使用javascript显示结果
- 使用javascript显示文件名上传
- 使用javascript显示隐藏的HTML元素
- 使用jquery和javascript显示PHP中的变量