2017-06-17 33 views
0

我想我的金額字段獲得總金額的轉換價值更新(金額我的意思是從所有複選框收到的金額總和選中)。例如,我有以下複選框。jquery貨幣兌換複選框點擊使用谷歌財經API

<input type="checkbox" value="10"> 
<input type="checkbox" value="20"> 
<input type="checkbox" value="30"> 

這些10,20和30(共60個)值以美元爲單位。我希望它被轉換成INR,並顯示在一個div中,如<div class="convertedAmount">Rs.0.00</div>。對於轉換,我在這裏使用Google Finance API,如下所示。

<?php 
$from_Currency = "USD"; 
$to_Currency = "INR"; 
$encode_amount = 1; 
$get = file_get_contents("https://www.google.com/finance/converter?a=$encode_amount&from=$from_Currency&to=$to_Currency"); 
$get = explode("<span class=bld>",$get); 
$get = explode("</span>",$get[1]); 
$converted_currency = preg_replace("/[^0-9\.]/", null, $get[0]); 
echo $converted_currency; 
?> 

現在的問題是,我希望每次複選框被選中或取消選中使用jQuery的時候更新div值。但是我對jQuery手無力,所以我遇到了麻煩。如果有幫助,我將不勝感激。請幫幫我。

回答

0

我發現瞭解決方案後,我發佈了一些問題。在這裏,我只是將轉換後的值存儲在變量inr中,並將其與從每個複選框中收到的總和值相乘,並將總和顯示在INR中。現在,每當選中或取消選中複選框時,都會分別更新這些值。

享受下面的解決方案代碼。它太快太:d

$(document).ready(function(){ 
    $('input[type=checkbox]').change(function(){ 
    var total = 0; var inr = '<?php echo $converted_currency; ?>'; 
    $('input:checkbox:checked').each(function(){ 
     total += isNaN(parseInt($(this).val())) ? 0 : parseInt($(this).val()); 
    }); 
    $("#costdisplay").html(total); 
    $("#inrdisplay").html(total*inr); 
    $("input[name=ap_amount]").val(total); 
    $("input[name=payment]").val(total); 
    $("input[name=amount]").val(total*inr); 
    }); 
}); 
1

你需要爲此的ajax。我花了一段時間,但這是你的解決方案。嘗試理解它並詢問你是否有任何問題。我還沒有完成其他兩個複選框,一旦你挑選了需要的複選框,但不難理解。 這裏是我把它叫做test100.php

<script type="text/javascript" language="javascript" src="jquery.min.js"></script> 


<input type="checkbox" class="checkbox" value="10"> 
<input type="checkbox" class="checkbox" value="20"> 
<input type="checkbox" class="checkbox" value="30"> 


<?php 
$from_Currency = "USD"; 
$to_Currency = "INR"; 
$encode_amount = 1; 
$get = file_get_contents("https://www.google.com/finance/converter?a=$encode_amount&from=$from_Currency&to=$to_Currency"); 
$get = explode("<span class=bld>",$get); 
$get = explode("</span>",$get[1]); 
$converted_currency = preg_replace("/[^0-9\.]/", null, $get[0]); 


echo "<div id='result'></br>$converted_currency</div>"; 
?> 

<script type="text/javascript"> 
$(".checkbox").click(function() { 

if($(this).is(":checked")) 
{ 
var rez = $("#result");  
var checkbox_value = $(this).val(); 

var dataString = 'checkbox_value=' + checkbox_value; 

$.ajax({ 
type: "POST", 
url: "calculation.php", 
data: dataString, 
cache: false, 

success: function(html) 
{ 

rez.empty().append(html); 
rez.fadeIn("slow") 
} 
}); 

} 
}); 
</script> 

上帝,我恨這個運動中的每一行代碼本網站頁面

這裏是位於頁面的計算:

<?php 

if($_POST['checkbox_value']) 
{ 
$from_Currency = "USD"; 
$to_Currency = "INR"; 
$encode_amount = $_POST['checkbox_value']; 
$get = file_get_contents("https://www.google.com/finance/converter?a=$encode_amount&from=$from_Currency&to=$to_Currency"); 
$get = explode("<span class=bld>",$get); 
$get = explode("</span>",$get[1]); 
$converted_currency = preg_replace("/[^0-9\.]/", null, $get[0]); 
echo "<div id='result'></br>$converted_currency</div>"; 

} 
?> 

你可以看到它在這裏的行動:ACTION :D

+0

感謝,但我已經想出了一個解決方案,而無需使用AJAX ..一個簡單的jQuery爲我做:d –

+0

也..你的代碼需要一定的時間來處理(近2秒),但只要點擊複選框就會處理一個.. –

+1

關於未選中的「未選中」,您提到了在未選中某個框的情況下,您希望結果具有什麼樣的價值? – 2017-06-17 13:52:49