如何在页面重新加载后保持复选框检查状态

时间:2016-04-28 16:57:35

标签: php jquery ajax laravel-4

你好我的laravel eshop我有复选框中的货币值列表。选中此复选框后,我的页面将重新加载并更新货币值,但页面重新加载后缺少复选框选择,任何想法。

<script>
  $(document).ready(function (){
  $(function() {  
   // Currency change in checkout
   $('#usd').on('ifChecked', function() {
       var currency = $("input[name='currency']:checked").val();
       $.ajax({
           type: 'POST',
           url: "{{URL::to('/')}}/cart/setcurrency",
           data: {'currency':currency},
           dataType: 'json',
           success: function(data) {  
               window.location.reload();  
               $('input#usd').html("input[name='currency']:checked");                  
           }
       })
   })
   $('#aed').on('ifChecked', function() {
       $("#tot_val").css("font-size", "14px");
       $("#tot_text").css("font-size", "14px");
       var currency = $("input[name='currency']:checked").val();
       $.ajax({
           type: 'POST',
           url: "{{URL::to('/')}}/cart/setcurrency",
           data: {'currency':currency},
           dataType: 'json',
           success: function(data) {
               window.location.reload();
               $('#aed').html("{{Session::get('currency')}}"); 
           }
       })
   })
   $("input[name='currency']:checked").bind( "change", function(event, ui)    {
    console.log('Lang: '+$(this).val());
      })
    </script>
    <div class="cart_currency">
                   <form action="#">
                       {{Session::get('currency')}}
                       <p>CURRENCY</p>
                       <input type="radio" name="currency" value="USD" id="usd">USD
                       <input type="radio" name="currency" value="AED" id="aed">AED

                       <a href="{{URL::to('/')}}/cart/shipping">
                           <input type="button" name="currency_sub" value="CONTINUE" class="currency_submit">
                       </a>
                   </form>
               </div>

    public function postSetcurrency(){
    $data = Input::all();
    Session::set('currency',$data['currency']);
    return Response::json('success');
}

2 个答案:

答案 0 :(得分:1)

您可以保存复选框的状态,并通过php或使用javascript设置本地存储将其作为POST传递 https://developer.mozilla.org/es/docs/Web/API/Storage/setItem

javascript:
//set the checkbox state
localStorage.setItem('checkbox_one','true');
localStorage.setItem('checkbox_one','false');


//on page load
if(localStorage.getItem('checkbox_one') == 'true'){
    //checkbox was checked
}else{
    //checkbox checked not true
}

答案 1 :(得分:1)

@if (Session::has('currency'))
     $value = Session::get('currency');
     @if(strcmp($value,"USD") == 0)
          <input type="radio" name="currency" value="USD" checked id="usd">USD
          <input type="radio" name="currency" value="AED" id="aed">AED
     @else if(strcmp($value,"AED") == 0)
          <input type="radio" name="currency" value="USD" id="usd">USD
          <input type="radio" name="currency" value="AED" checked id="aed">AED
     @endif
@else
     <input type="radio" name="currency" value="USD" id="usd">USD
     <input type="radio" name="currency" value="AED" id="aed">AED
@endif

我之前没有使用过Laravel,但只是看一些场外示例,你可以尝试一下。它应该替换你当前的单选按钮。