2013-09-22 57 views
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') } 
     } 

    } 

完成。感謝您的幫助

+0

你使用

0
var selectedPica = document.getElementsByTagName('pica'); 

getElementsByTagName作品不是這樣的。

例如:

<script> 
    function getElements() 
     { 
     var x=document.getElementsByTagName("input"); 
     alert(x.length); 
     } 
    </script> 

    <input type="text" size="20"><br> 
    <input type="text" size="20"><br> 
    <input type="text" size="20"><br><br> 
    <input type="button" onclick="getElements()" value="How many input elements?"> 

什麼ü正在尋找是getElementsByName

function getElements() 
{ 
var x=document.getElementsByName("x"); 
alert(x.length); 
} 
</script> 
Cats: 
<input name="x" type="radio" value="Cats"> 
Dogs: 
<input name="x" type="radio" value="Dogs"> 

<input type="button" onclick="getElements()" value="How many elements named 'x'?"> 
+0

只是一個提示,它不是時髦的鏈接到'w3schools'。您可能想鏈接到更好的網站。 – Itay

0

改變線VAR VAR selectedPica = document.getElementsByTagName( '異食癖');到selectedPica = document.getElementsByName('pica');

0
<form id="order" name="orderForm"> 
var selectedPica = document.getElementsByTagName('pica'); 

替換爲:

var selectedPica = document.forms['orderForm'].elements['pica']; 

DEMO

1

您腳本幾乎是很好,只是用getElementsByName,而不是... ByTagName已經提到。

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'); 
    var res = document.getElementById('result').value; 
    for(var i=0; i<selectedPica.length; i++){ 
     if(selectedPica[i].checked) { 
      alert('selected'); 
     } 
     else { alert('not selected') } 
    } 

} 

此外,腳本引用結果項目。你的HTML標記不會提供這一點,只需添加一個div id爲「結果」 ......

<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> 
       <td><div id="result"></div> 
    </tr> 
</table> 

+0

結果在代碼中,此代碼只是片段。但我似乎不能讓我的功能工作 – MrPaulius

+0

它只是工作正常,http://jsfiddle.net/Elak/mcEQa/ 因爲如果你的發佈代碼是你的HTML頁面的全部內容,你必須把你的JavaScript到一個標籤 – MichaC