2013-08-02 113 views
0

我想使用if else語句來執行某個動作,如果選擇了一個單選按鈕並且執行另一個動作(如果選擇了另一個動作)。在這種情況下,如果選擇了基本按鈕,我想使用一個公式,如果選擇了pro按鈕,則需要使用pro公式。我是編程新手,所以對進階的道歉。使用if語句與javascript中的單選按鈕

<html> 

<head> 

<title>Calculator</title> 

<script language="javascript" type="text/javascript"> 
function packageTotal(){ 
    //Enter in prices here 
    var x = 5; 
    var y = 10; 
    var p = x + y*12; 
    var b = y * 12; 

    if(document.calculator.getElementById('basicProgram').checked) { 
     //Basic package is checked 
     document.calculator.total.value=b; 


    }else if(document.calculator.getElementById('proProgram').checked) { 
     //Pro package is checked 
     document.calculator.total.value=p; 

    } 

</head> 

<body> 

<!-- Opening a HTML Form. --> 
<form name="calculator"> 

<!-- User fills out form here --> 

<br /> 
<input type="radio" name="programType" id="basicProgram" value="Basic" /> Basic 
<input type="radio" name="programType" id="proProgram" value="Pro" checked /> Pro 

<!-- Here result will be displayed. --> 

<br /> 
Your total price is: <input type="text" name="total"> 


<input type="button" value="Submit" onclick="javascript:packageTotal();"> 


</form> 

</body> 
</html> 
+0

有一些錯別字..這是我固定的。這裏是[** fiddle **](http://jsfiddle.net/Pn6X7/)。 –

+0

非常感謝你!問題解決了! –

回答

1

看起來像你的JavaScript缺少}

<html> 

<head> 
<title>Calculator</title> 

<script language="javascript" type="text/javascript"> 
function packageTotal() { 
    // Enter in prices here 
    var x = 5; 
    var y = 10; 
    var p = x + y * 12; 
    var b = y * 12; 

    if (document.getElementById('basicProgram').checked) { 
     // Basic package is checked 
     document.calculator.total.value = b; 

    } else if (document.getElementById('proProgram').checked) { 
     // Pro package is checked 
     document.calculator.total.value = p; 

    } 
} 
</script> 
</head> 

<body> 

<!-- Opening a HTML Form. --> 
<form name="calculator"> 

<!-- User fills out form here --> 

<br /> 
<input type="radio" name="programType" id="basicProgram" value="Basic" /> Basic 
<input type="radio" name="programType" id="proProgram" value="Pro" checked /> Pro 

<!-- Here result will be displayed. --> 

<br /> 
Your total price is: <input type="text" name="total"> 


<input type="button" value="Submit" onclick="javascript:packageTotal();"> 


</form> 

</body> 
</html> 

演示:Fiddle

+0

爲什麼返回false?那是按鈕 –

+0

@MohammadMasoudian錯過了 –

0

裏面的功能packageTotal(),你也將要這樣寫:

//stop the form from submitting 
//place this wherever you need it in your logic 
return false; 

我還以爲你有興趣增加的onChange()的單選按鈕代替的onsubmit方法:

<input type="radio" name="programType" id="basicProgram" value="Basic" onChange="packageTotal()" /> Basic 
<input type="radio" name="programType" id="proProgram" value="Pro" checked onChange="packageTotal()" /> Pro 
0
<html> 

<head> 

<title>Calculator</title> 

<script language="javascript" type="text/javascript"> 
function packageTotal(){ 
    //Enter in prices here 
    var x = 5; 
    var y = 10; 
    var p = x + y*12; 
    var b = y * 12; 

    if(document.getElementById('basicProgram').checked) { 
     //Basic package is checked 
     document.calculator.total.value=b; 


    }else if(document.getElementById('proProgram').checked) { 
     //Pro package is checked 
     document.calculator.total.value=p; 

    } 
} 
</head> 

<body> 

<!-- Opening a HTML Form. --> 
<form name="calculator"> 

<!-- User fills out form here --> 

<br /> 
<input type="radio" name="programType" id="basicProgram" value="Basic" /> Basic 
<input type="radio" name="programType" id="proProgram" value="Pro" checked /> Pro 

<!-- Here result will be displayed. --> 

<br /> 
Your total price is: <input type="text" name="total"> 


<input type="button" value="Submit" onclick="javascript:packageTotal();"> 


</form> 

</body> 
</html>