我很新的JavaScript編碼,我搜索了一些帖子,無法得到它的工作,所以很抱歉,如果它是一個簡單的答案。隱藏HTML基於下拉值(香草)
我需要根據是否在下拉菜單中選擇一個選項來顯示div元素。需要內聯,香草js(沒有jQuery)。這是我的,但有些不起作用。
的Java:
<script type="text/javascript">
var repeatCombo = document.getElementById("repeatSelect");
var repeatVal = repeatCombo.options[repeatCombo.selectedIndex].text;
function repeatCheck() {
if (repeatVal = 'Repeat Deposit') {
document.getElementById('repeatGroup').style.display = 'block';
}
else document.getElementById('repeatGroup').style.display = 'none';
}
</script>
HTML:
<div class="form-group">
<label class="col-md-4 control-label" for="repeatSelect">Initial/Repeat Deposit</label>
<div class="col-md-4">
<select name="repeatSelect" class="form-control input-md" id="repeatSelect" onchange="javascript:repeatCheck();">
<option value="Initial Deposit" >Initial Deposit</option>
<option value="Repeat Deposit" >Repeat Deposit</option>
</select>
</div>
</div>
<div id="repeatGroup" style="display: none;">
<label class="col-md-4 control-label" for="repeatDepositInfo">Date of Initial Deposit</label>
<div class="col-md-4">
<input name="repeatDepositInfo" class="form-control input-md" id="repeatDepositInfo" type="date" placeholder="">
</div>
</div>
** 1)**您正在使用的賦值運算符,而不是比較操作。您應該使用'repeatVal =='重複存款'。 ** 2)**您必須將'var repeatCombo = ...'和'var repeatVal = ...'INSIDE函數放入函數,因爲'repeatVal'將會改變,'repeatCombo'不會存在,直到整個HTML文檔被加載。當*函數被調用時它將*存在。 –