0
不知道爲什麼此功能無法啓動。經過它,讀了很多關於SO,沒什麼...... 我敢肯定有非常小失誤的地方,在那裏我不能當場..通過單選按鈕循環,功能不起作用
<form id="order">
<table>
<tr>
<td>
What type of pica would you like?<br>
Vegetarian, £6.50 <input type="radio" name="pica" onclick="calcul();" value="vegetarian"><br>
Meat Lover, £7.20 <input type="radio" name="pica" onclick="calcul();" value="meatLover"><br>
Hawaian, £5.5 <input type="radio" name="pica" onclick="calcul();" value="hawaian">
<br><br>
</td>
</tr>
</table>
</form>
var picaPrice = new Array();
picaPrice['vegetarian']=6.5;
picaPrice['meatLover']=7.2;
picaPrice['hawaian']=5.5;
function calcul(){
var pprice = 0;
var selectedPica = document.getElementsByName('pica');
for(var i=0; i<selectedPica.length; i++){
if(selectedPica[i].checked) {
alert('selected');
}
else { alert('not selected') }
}
}
完成。感謝您的幫助
你使用
getElementsByTagName作品不是這樣的。
例如:
什麼ü正在尋找是getElementsByName
來源
2013-09-22 09:45:57 Roar
只是一個提示,它不是時髦的鏈接到'w3schools'。您可能想鏈接到更好的網站。 – Itay
改變線VAR VAR selectedPica = document.getElementsByTagName( '異食癖');到selectedPica = document.getElementsByName('pica');
來源
2013-09-22 09:47:22 Vineesh
替換爲:
見DEMO
來源
2013-09-22 09:50:09
您腳本幾乎是很好,只是用getElementsByName,而不是... ByTagName已經提到。
此外,腳本引用結果項目。你的HTML標記不會提供這一點,只需添加一個div id爲「結果」 ......
來源
2013-09-22 09:53:28 MichaC
結果在代碼中,此代碼只是片段。但我似乎不能讓我的功能工作 – MrPaulius
它只是工作正常,http://jsfiddle.net/Elak/mcEQa/ 因爲如果你的發佈代碼是你的HTML頁面的全部內容,你必須把你的JavaScript到一個標籤 – MichaC