在我的代码中,我有一个选择字段和两个只读字段。我需要默认隐藏只读字段(加载页面时),如果从选择字段中选择任何值,则需要显示这些字段。选择字段的值是使用php从mysql数据库获取。
<select id ="name" name="name" class="form-control" required>
<option value="" disabled selected>Choose Wallet</option>
<?php
//my query
echo '<option value="' . $row['group'] . '">' . $row['group'] . '</option>';
?>
</select>
<input id ="od" value="<?php echo $od ?>" class="form-control" readonly/>
<input id="bal" value="<?php echo $bal ?>" class="form-control" readonly/>
答案 0 :(得分:0)
尝试
function onNameChange() {
var name = document.getElementById('name');
document.getElementById('od').style.display = name.value ? 'inline-block' : 'none'
document.getElementById('bal').style.display = name.value ? 'inline-block' : 'none'
}
&#13;
<!-- use a change event handler for the select -->
<select id="name" name="name" class="form-control" onchange="onNameChange()" required>
<option value="" disabled selected>Choose Wallet</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<!-- Hide the inputs using `display: none` -->
<input id="od" value="<?php echo $od ?>" class="form-control" style="display: none" readonly/>
<input id="bal" value="<?php echo $bal ?>" class="form-control" style="display: none" readonly/>
&#13;