2017-04-05 77 views
0

我們可以直接將值設置爲我們在文本框中想要像
<input type="text" name="total" value="500" size="10" readonly />
值是否有任何顯示默認文本檢查值的方法沒有使用value =「500」的盒子?組默認選中在總費用顯示值不使用「值」

var checks = document.getElementsByName('deliveryType'); 
 
for (var i = 0; i < checks.length; i++) checks[i].onclick = function() { 
 
    var cal = document.getElementsByName('deliveryType'); 
 
    var total = 0; 
 
    for (var i = 0; i < cal.length; i++) { 
 
    if (cal[i].checked) { 
 
     total += parseFloat(cal[i].getAttribute("data-price")); 
 
    } 
 
    } 
 
    document.getElementsByName("total")[0].value = "$" + total.toFixed(2); 
 
}
<form id= selectEvent> 
 
<section id="collection"> 
 
\t \t \t 
 
\t \t \t <p> 
 
\t \t \t Home - &pound;500.00 <input type="radio" name="deliveryType" value="home" data-price="500.00" checked />&nbsp; | &nbsp; 
 
\t \t \t self collect - no charge <input type="radio" name="deliveryType" value="ticketOffice" data-price="0" /> 
 
\t \t \t </p> 
 
\t \t </section> 
 
    
 
    <section id="checkCost"> 
 
\t \t \t <h2>Total cost</h2> 
 
\t \t \t Total <input type="text" name="total" size="10" readonly /> 
 
\t \t </section> 
 
    </form>

+1

'InputTextElement.defaultValue =「whatever''? – PHPglue

+0

你可以請示範? – ccs

回答

1

嘗試以下代碼:

var checks = document.getElementsByName('deliveryType'); 
 
checks.forEach(function(value){ 
 
    if(value.checked){ 
 
     document.getElementsByName("total")[0].value = "$" + value.getAttribute('data-price'); 
 
    } 
 
}) 
 
for (var i = 0; i < checks.length; i++) checks[i].onclick = function() { 
 
    var cal = document.getElementsByName('deliveryType'); 
 
    var total = 0; 
 
    for (var i = 0; i < cal.length; i++) { 
 
    if (cal[i].checked) { 
 
     total += parseFloat(cal[i].getAttribute("data-price")); 
 
    } 
 
    } 
 
    document.getElementsByName("total")[0].value = "$" + total.toFixed(2); 
 
    
 
}
<form id= selectEvent> 
 
    <section id="collection">  
 
     <p> 
 
     Home - &pound;500.00 <input type="radio" name="deliveryType" value="home" data-price="500.00" checked />&nbsp; | &nbsp; 
 
     self collect - no charge <input type="radio" name="deliveryType" value="ticketOffice" data-price="0" /> 
 
     </p> 
 
    </section> 
 

 
<section id="checkCost"> 
 
     <h2>Total cost</h2> 
 
     Total <input type="text" name="total" size="10" readonly /> 
 
    </section> 
 
</form>

+0

這是工作!是否檢查是否有任何無線電廣播?或者可以請你簡單地爲我解釋一下嗎? – ccs

+1

只需循環所有選中的單選按鈕即可。如果找到任何選中的單選按鈕,則抓取該無線電的「數據價格」值。然後簡單地將其分配到總數中。希望清除代碼。謝謝。 – Mamun