简体   繁体   English

页面刷新后,OnChange选项保持不变

[英]OnChange option remain same after page refresh

I have this script for money conversion so user can choose it's currency from the list like Us to Euro so I want to make it remain same after page refresh like if user have chosen Euro and he refresh the page it should remain same. 我有这个脚本进行货币转换,所以用户可以从像我们到欧元的列表中选择它的货币所以我想让它在页面刷新后保持相同,就像用户选择了欧元并且他刷新页面它应该保持相同。

Here is my Javascript and Code 这是我的Javascript和代码

<script>
function updatePrice(val) {
    p = document.getElementById("original_price").value;
    newp = p * val;
    document.getElementById("calculated_price").value = newp;
}

</script>

Php Code: PHP代码:

 <?php  
    $pr = 180;
    ?>
    <select onchange="updatePrice(this.value)">
        <option value="1">US</option>
        <option value="98">RS</option>
        <option value="61">Ind</option>
    </select>
    <input type="hidden" id="original_price" value="<?php echo $pr; ?>" />
    Price: <input type="text" id="calculated_price" value="<?php echo $pr; ?>" />

Update 1 After Implementing Session 实现会话后更新1

<?php
session_start();
// store session data
$_SESSION['value']=".updatePrice(this.value).";

if(isset($_SESSION['value']));

?>

<?php  

$pr = 180;

?>
<select onchange="<?php echo $_SESSION['value']; ?>">
    <option value="1">US</option>
    <option value="98">RS</option>
    <option value="61">Ind</option>
</select>

<br>
<hr>


<input type="hidden" id="original_price" value="<?php echo $pr; ?>" />
Price: <input type="text" id="calculated_price" value="<?php echo $pr; ?>" />


<script>
function updatePrice(val) {
    p = document.getElementById("original_price").value;
    newp = p * val;
    document.getElementById("calculated_price").value = newp;
}

</script>

Actually PHP dosent offer any viewstate mechanism, as far as i know , So what you can do is store this in some hidden field.The best way and my personal recommendation is to use a session variable for this purpose 实际上PHP并不提供任何视图状态机制,据我所知,所以你可以做的是将它存储在一些隐藏的领域。最好的方法和我的个人建议是为此目的使用会话变量

http://www.w3schools.com/Php/php_sessions.asp http://www.w3schools.com/Php/php_sessions.asp

And if you need to solve this issue using javascript, You can use Cookies too 如果您需要使用javascript解决此问题,您也可以使用Cookie

http://www.w3schools.com/js/js_cookies.asp http://www.w3schools.com/js/js_cookies.asp

I have done this using jquery and javascript by setting a cookie, hence i dont want you to get confused with jquery plugin for cookie. 我通过设置cookie使用jquery和javascript完成了这个,因此我不希望你与cookie的jquery插件混淆。 You can do this in a much more simpler way using jquery plugin for cookie. 您可以使用jquery插件为cookie更简单地执行此操作。 Here's the code 这是代码

HTML HTML

<select id="selectCurrency">
    <option value="1">US</option>
    <option value="98">RS</option>
    <option value="61">Ind</option>
</select>

jquery/javascript jQuery的/ JavaScript的

$(document).ready(function(e){
 var name = "Currency=";
 var ca = document.cookie.split(';');
 for(var i=0; i<ca.length; i++){
    var c = ca[i].trim();
    if (c.indexOf(name)==0) $('#selectCurrency').val(c.substring(name.length,c.length));
  }
});

$('#selectCurrency').change(function(e){
    var cookieVal = "Currency="+$(this).val();
    document.cookie = cookieVal ;

  });

Fiddle 小提琴

http://jsfiddle.net/AmarnathRShenoy/HM3Zj/ http://jsfiddle.net/AmarnathRShenoy/HM3Zj/

You can use session or store the selected values somewhere in database, inUpdate price function make an ajax call which stores, your selected value and keep pdating at, on onchange event. 您可以使用会话或将所选值存储在数据库中的某个位置,在更新价格函数中进行ajax调用,在onchange事件中存储,您选择的值并保持pdating。 now, each time your page gets refreshed, the previuosly selected value will get fetched from the database and you can show it seleted. 现在,每次刷新页面时,都会从数据库中获取先前选择的值,然后您可以将其显示为seleted。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM