php - 刷新时输入值不会更改为其默认值

标签 php javascript jquery html

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

<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 标记显示初始值。为什么?

欢迎任何帮助/建议。

最佳答案

$(document).ready(function(){

之后写两条语句

喜欢:

$(document).ready(function(){

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

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

关于php - 刷新时输入值不会更改为其默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18572215/

相关文章:

php - 如何在不使用 OAuth2 或永久 key 的情况下在 PHP 中使用 BigQuery streaming insertall?

javascript - 如何使用 AJAX 和 php 将多个文件上传到服务器

javascript - Angular ng-repeat orderBy不起作用

javascript - 在 node.js 中使用 Firebase 3.0 进行身份验证

javascript - Wordpress,以正确的方式添加自定义脚本

jquery - 如何每隔几秒用 Jquery 循环一个 css 背景图像?

php - 一个查询中有两个 mysql 选择 - 一个接一个

javascript - 哪里可以在JavaScript中使用空值运算符?

jQuery .load() 查询/性能

php - 将laravel 4.2项目导入eclipse