我想根據單選按鈕中的選擇來隱藏或顯示我的微調框。微調器顯示在一張桌子上。如果用戶選擇是(單選按鈕),它將顯示微調器所在的表格,如果用戶選擇了否(單選按鈕),它將隱藏微調器所在的表格。我的表格在使用javascript時未隱藏
我研究了Google在JavaScript中使用樣式的最佳方式,我最常見的一種方法是使用(variable).style.(css property)
。
我試過這個,但它沒有奏效。我想知道的是,我是否編寫了這個錯誤(它沒有在錯誤控制檯中顯示任何內容,並且它看起來正確,我遵循了這些示例)還是需要以另一種方式進行顯示和隱藏表格?
下面是我的HTML代碼:
<table id="tblWeight">
<tr>
<th>6: Provide a Total Weight for your Session</th>
<td><input type="radio" name="weightChoice" value="yes" onClick="getWeight()"/> Yes</td>
<td><input type="radio" name="weightChoice" value="No" onClick="getWeight()"/> No</td>
</tr>
</table>
<div id="radioAlert"></div>
<br/>
<table>
<tr>
<th>Weight (%):</th>
<td class="spinner"><input type="text" class="spinnerWeight" name="txtWeight" id="txtWeight"></td>
<td><button class="scrollBtn" id="btnWeightUp" type="button"><img src="Images/black_uppointing_triangle.png" alt="Increase" /></button>
<button class="scrollBtn" id="btnWeightDown" type="button"><img src="Images/black_downpointing_triangle.png" alt="Decrease" /></button></td>
</tr>
</table>
<div id="weightAlert"></div>
<br/>
<table><tr><th>7: Module:</th>
<td><?php echo $moduleHTML; ?></td>
</tr>
</table>
下面是JavaScript代碼:
function getWeight() {
var weightChoice = document.getElementsByName("weightChoice");
var textWeight = document.getElementById("txtWeight");
var tableWeight = document.getElementById("tblWeight");
if(weightChoice[0].checked == true){
tableWeight.style.visibility=="visible";
textWeight.value == 0;
} else {
tableWeight.style.visibility=="hidden";
textWeight.value == 0;
}
}
有些情況下if(x == true)'不會提供與if(x)'相同的結果,所以我不會太快推薦在任何地方使用它。 – RightSaidFred
@RightSaidFred:比如......?我認爲你用標識符'==='混淆了不平等的=='。 (我知道,有些情況像'someObject.property = a == true;'這樣的賦值情況,但這與'someObject.property = Boolean(a);'或其他各種更清晰的事情是一樣的。 ) – Ryan
因爲鬆散等式執行'toNumber'轉換,而'if(x)'將使用'toBoolean',所以可能會得到不同的結果。我能想到的兩個例子是與只有空格的字符串進行比較,並與空數組進行比較。 'toBoolean'對於這些將是'true',但'toNumber'將會產生'0',它不是'== true'。 – RightSaidFred