duanniying2342 2017-07-20 15:00
浏览 79
已采纳

onClick和onChange与站点重新加载

I have made a Formula with on onchange="this.form.submit()" and a automatic page refresh function with window.location.replace();. My Problem is, that i need to know where the user clicked in the last time. My Solution was a second onclick="" event to get the id of the selected field. This works fine if i make no changes in the form. When i change some values and go to a other field, the onclick function didn't work. How can i solve this problem? And by the way, sorry for my bad english.

    <input name="'.$idname.'" value="'.$cont_field.'" type="text" class="loginField" size="'.$breite.'" style="width:98%;" id="'.$idname.'" onchange="this.form.submit()" onclick="focusCookie(event)">

    <script type="text/javascript">
    function focusCookie (event) {
        event = event || window.event;
        var target = event.target || event.srcElement;
        var anch = target.id;
        createCookie("anchor", anch, new Date(new Date().getTime() + 10000));
    }
    function createCookie(name, value, expires, path, domain) {
      var cookie = name + "=" + escape(value) + ";";
      if (expires) {
        if(expires instanceof Date) {
          if (isNaN(expires.getTime()))
           expires = new Date();
        }
        else
        expires = new Date(new Date().getTime() + parseInt(expires) * 1000 * 60 * 60 * 24);
        cookie += "expires=" + expires.toGMTString() + ";";
      }
      if (path)
        cookie += "path=" + path + ";";
      if (domain)
        cookie += "domain=" + domain + ";";
      document.cookie = cookie;
    }
    </script>

And this ist the loadingfunction

    //--> Loaders
    function Loader($https_url,$seite,$loader_id){
    $loaderurl = '<script type="text/javascript">window.location.replace("'.$https_url.'/index.php?inhalt=extranet&extra=formular&seite='.$seite.'#'.$loader_id).'");</script>';
    return $loaderurl;  
    }

Ok 2 Problems solved and 1 still pending. I can send the form and catch the anchor tag befor. But after the Siteload the focus(); is lost...

Here my Code:

    <script type="text/javascript">
    function submitform (event) {
    event = event || window.event;
    var target = event.target || event.srcElement;
    var anch = target.id;
    createCookie("anchor", anch, new Date(new Date().getTime() + 10000));
    sendeForm();
    document.getElementById(anch).reload();
    }
    function createCookie(name, value, expires, path, domain) {
    var cookie = name + "=" + escape(value) + ";";
    if (expires) {
    if(expires instanceof Date) {
    if (isNaN(expires.getTime()))
    expires = new Date();
    }
    else
    expires = new Date(new Date().getTime() + parseInt(expires) * 1000 * 60 * 60 * 24);
    cookie += "expires=" + expires.toGMTString() + ";";
    }
    if (path)
    cookie += "path=" + path + ";";
    if (domain)
    cookie += "domain=" + domain + ";";
    document.cookie = cookie;
    }
    function sendeForm(){
    document.getElementById("ID").submit(); 
    }
    </script>
  • 写回答

1条回答 默认 最新

  • ds34222 2017-07-23 15:36
    关注

    I solved it with a AJAX Form by sending the Data with a XMLHttpRequest. This works fine. The Variables are generated with PHP.

        function ajax_post(){
        //Vars
        var xhr = new XMLHttpRequest();
        var url = 'URL';
        <?php echo $VARLIST; ?>
        var ausgabe = <?php echo $VARS; ?>;
        //Send
        xhr.open("POST", url, true);
        xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xhr.onreadystatechange = function(){
        if(xhr.readyState == 4 && xhr.status == 200){
        var return_data = xhr.responseText;
        document.getElementById("Register").innerHTML = return_data;
        }
        }
        xhr.send(ausgabe);
        document.getElementById("Register").innerHTML = "Reload..";
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么