簡體   English   中英

單選按鈕更改JavaScript中的變量

[英]Radio buttons change variable in JavaScript

我有兩個用於計算總計的字段,但是我有一個單選按鈕對,用於確定方程式中的變量。 這是單選按鈕代碼:

<input type="radio" name="estimate_pool_shape" value="1" /> Angle
<input type="radio" name="estimate_pool_shape" value=".86392" /> Freeform

之后,此JavaScript隨后根據這些字段和腳本來計算池的表面積:

    (Length <input type="text" size="5" name="estimate_pool_length" 
onBlur="conv2inches(document.forms.mainform);" />) x 
    (Width <input type="text" name="estimate_pool_width" size="5" 
onBlur="conv2inches(document.forms.mainform);" />)

以下JavaScript已可用於更新字段,我只需要它用於設置字段值的變量即可根據單選按鈕進行更改。

        function conv2inches(mainform) {            

        var oin;    
        var oinches;

        if(isNaN(mainform.estimate_pool_width.value)||isNaN(mainform.estimate_pool_length.value)){  
            alert("Please enter numbers only"); 
            return false;   
        }

        oin = ((mainform.estimate_pool_width.value) + (mainform.estimate_pool_length.value) * %%%RADIO BUTTON VARIABLE HERE%%%);    
        oinches = (oin);    

        if(oinches==0){ 
            alert("Please enter valid values into the boxes");  
        }   
            mainform.estimate_pool_sqft.value = oinches;    
            return false;
        }
var estimate_pool_shape = 0; // Or some default value.  Maybe 1?
for(var i = 0; i < mainform.estimate_pool_shape.length;i++){
 if(mainform.estimate_pool_shape[i].checked){
   estimate_pool_shape = mainform.estimate_pool_shape[i].value;
   break;
 }
}

應該給你你所需要的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM