以下功能似乎沒有工作..單選按鈕選中的JavaScript - 沒有jQuery的
function myCalculator() {
var operator = document.getElementsByName("operator");
var output = document.getElementById("output");
var firstDig = parseInt(document.getElementById("firstDig").value);
var secondDig = parseInt(document.getElementById("secondDig").value);
for (var i=0; i<operator.length; i++){
var currentOperator = operator[i].id;
if(currentOperator.checked == "Add"){
var add = firstDig + secondDig;
output = add;
};
};
}
莫名其妙的「如果」參數,返回false,但我得到了與該屬性的元素檢查...即時決策一個簡單的計算器...以下是HTML ...
<fieldset>
<label>Enter Number:</label>
<input type="text" id="firstDig" />
<input type="text" id="secondDig" />
<br/> <br/>
<label>Select Operator:</label>
<br/><br />
<input type="radio" id="Add" name="operator" checked="checked" />
<label for = "Add">Add</label>
<input type="radio" id="Subt" name="operator" />
<label for = "Subt">Subtract</label>
<input type="radio" id="Divi" name="operator" />
<label for = "Divi">Divide</label>
<input type="radio" id="Multi" name="operator" />
<label for = "Multi">Multiply</label>
</fieldset>
<fieldset>
<p>RESULT</p>
<input type="text" id="output" />
<button type="button" onclick="myCalculator()">Calculate</button>
</fieldset>
我還在學習JavaScript所以我不會的,在目前jQuery中inetrested基礎......所有的答案都感激.. 。比ks
@DhavalMarthak是你的權利 –
''currentOperator本身是抱着'id'所以shud be'currentOperator ==「添加」 – Zee
@ Sourabh-是剛剛更正 –