1

我正在使用谷歌货币转换器。一切都很好,但在页面刷新时我无法将输入值设置为其原始值。下面是我的代码。

<script src = "js/CurrencyConverter.js" type="text/javascript"></script>
<select id="FromCurrency" class="CurrencyDropDown"></select>        
<input type = "text" id = "UnitPrice" value="enter amount"/>
</br>
<select id="ToCurrency" class = "CurrencyDropDown"></select>        
<input type = "text" id = "destinationPrice" value="result"/>
<script>
    $(document).ready(function(){
        $('select#ToCurrency').change(function(){
            convertcurrency();
        })

    });
    function convertcurrency(){
        var url = $('input[type=hidden]').val();
        var priceunit = $('input#UnitPrice').val();// alert(priceunit);
        var fromcurrencycode = $('select#FromCurrency').val(); //alert(fromcurrencycode);
        var tocurrencycode = $('select#ToCurrency').val();// alert(tocurrencycode);
        ConvertCurrency(url, priceunit,fromcurrencycode,tocurrencycode);        
    }
    function ConvertCurrency(Url , PriceUnit,fromCurrencyCode,toCurrencyCode){ 
        $.ajax({
            url: '<?php echo site_url('curchange')?>',
            type : 'POST',
            dataType: "html",
            data : {unitprice : PriceUnit, fromcode : fromCurrencyCode, tocode : toCurrencyCode},
            success: function (data) {
                if(data != '')
                    $('input#destinationPrice').val(data);
                else
                    alert('Cannot convert');
            },
            error :  function(){
                alert('Error in Loading Data');
            }


        });
        return false;
    }
</script>

在 CurrencyConverter.js 中,我列出了在窗口加载时加载到下拉列表的国家/地区列表。

curchange.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    Class Curchange extends CI_Controller{

        function __construct() {
            parent::__construct();
        }

        function index(){
            $unit = $this->input->post('unitprice');
            $from = $this->input->post('fromcode');
            $to = $this->input->post('tocode');
            $url = 'http://www.google.com/ig/calculator?hl=en&q='.$unit.$from.'=?'.$to;
            $ch = curl_init();
    $timeout = 0;
    curl_setopt ($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch,  CURLOPT_USERAGENT , "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $rawdata = curl_exec($ch); 
    curl_close($ch);
    $data = explode('"', $rawdata);
        $error = explode(' ', $data[5]);
        if(empty($error[0])){
            $data = explode(' ', $data[3]);
            $var = round($data[0], 3);
        }else{
            $var = '';
        }
        echo $var;
        }

    }
?>

代码工作正常。货币正在转换,但是当我更改货币然后刷新页面而不是将输入值显示为“输入金额”和“结果”时,输入 html 标记显示初始值。为什么?

欢迎任何帮助/建议。

4

1 回答 1

0

后面写两句$(document).ready(function(){

喜欢:

$(document).ready(function(){

  $("#UnitPrice").val("enter amount");
  $("#destinationPrice").val("result");
 });

它将在页面刷新时设置默认值

于 2013-09-02T11:40:27.573 回答