切换锚标记的值并将其传递给网址

时间:2017-09-21 13:13:06

标签: javascript jquery html ajax

我想在ajax url中传递或关闭值作为trans。现在正在切换文本。但是,根据on和off值,不会更新trans in url。有什么帮助吗?我需要重新加载页面,因为我在我的代码中根据这个开启和关闭的值来设置和取消设置。所以当链接值为“on”时,值应该在url中传递并执行基于动作的操作在那个..另外基于off的动作..在控制台中它只显示“on”值。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
              $(document).ready(function() {
                  $('#trans_flag').click(function() {
    
                      var trans = $('#trans_flag').text() ;
                      //var trans=$(this).val();
                      if(trans=='on')
                      {
                          $('#trans_flag').text("off")
                          trans='on';
                      }
                      else
                      {
                          $('#trans_flag').text("on")
                          trans='off';
                      }
                      var base_url = '<?=base_url()?>';
                      $.ajax
                      ({
                          'type': 'POST',
                          'url': base_url + 'translation/language/searchUntranslated/'+trans,
                          'success': function(response){
                              window.location.reload();
                              alert(trans);
                              console.log(response);
                          }
                      });
    
                  });
                  });
              </script>
    <a  id='trans_flag' name='trans_flag'>on</a>

2 个答案:

答案 0 :(得分:0)

在重新加载页面后,使用localstorage维护状态onoff。请在本地Failed to read the 'localStorage' property from 'Window'尝试。

$(document).ready(function() {
    function getTrans(){
      var trans = localStorage.getItem('trans');
        if(trans !=null){
          if(trans == 'on'){
            $('#trans_flag').text("off");
            localStorage.setItem('trans', 'off');
          }else{
            $('#trans_flag').text("on");
            localStorage.setItem('trans', 'on');
          }
        }else{
            trans = 'on';
            $('#trans_flag').text("on");
            localStorage.setItem('trans', 'on');
        }
        return trans;
    }
    $('#trans_flag').click(function() {
        var trans = getTrans();
        var base_url = 'base_url/';
        console.log(base_url + 'translation/language/searchUntranslated/'+trans);
    });
    getTrans();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a  id='trans_flag' name='trans_flag'></a>

答案 1 :(得分:-1)

删除警报(trans)形成代码ajax请求

 $.ajax
                  ({
                      'type': 'POST',
                      'url': base_url + 'translation/language/searchUntranslated/'+trans,
                      'success': function(response){
                          window.location.reload();
                          //alert(trans);
                          console.log(response);
                      }
                  });