简体   繁体   English

刷新后留在同一页面上

[英]stay at the same page after refresh

i have a page containing a form, there's a javascript listener on input type select , the event is onchange, depending on the option chosen from the select the page will display a block of inputs (div), i am using hide and show to display or hide a block of html elements, one of those divs (div 2) has an input type select also with onchange event with ajax call . 我有一个包含表单的页面,输入类型为select的javascript侦听器,该事件为onchange,具体取决于从选择页面中选择的选项,该页面将显示一个输入块(div),我正在使用隐藏和显示来显示或隐藏html元素块,这些div之一(div 2)的输入类型也会与ajax call的onchange事件一起选择。 my first problem is that after hitting validate button of the form browser brings the default div i want that he stay at the last previous div , second problem is that div 2 after refresh the ajax call does not bring any data , its logical since the event is on change. 我的第一个问题是,在单击表单浏览器的验证按钮后,我带来了默认的div,我希望他停留在上一个div;第二个问题是,刷新ajax调用后的div 2没有带来任何数据,这是自事件以来的逻辑在变化。 so want if div 2 has been displayed , the ajax call works after refresh . 因此,如果已显示div 2,则希望刷新后可以执行ajax调用。

this my html code 这是我的html代码

 <form id="boxpanel" class="form-panel" method='POST' enctype='multipart/form-data' action='<?php echo URL.htmlspecialchars('ads');?>'>

      <div>

     <label for='catego'><b>categories: </b><span>(*)</span></label>
     <span><select name="catego" id="catego">
     <option value='0'>select a category </option>
      <?php
  foreach($this->categories as $key => $value) { ?>

      <option value=<?php echo $value['id'];?>  <?php if (isset($_POST['catego']) && $_POST['catego'] == $value['id']){ echo 'selected';}else{ echo '';}?>> <?php echo $value['categorie'] ;?></option>


        <?php 

    }
?>  
      </select><br /></span>

 </div>


 <div id="div12" class="hide">
<br />
 <div>
            <label for='mark'><b>cars:</b> <span>(*)</span></label>
      <select  name='cars' id='cars' onchange="showHint(this.value)">
       <option value=0>s1</option>
        <option value=2>s2</option>
         <option value=3>s3</option>


      </select>
      </div>
      <br />
      <div>
      <label for='categos'><b>Models:</b></label>
     <select id="txtHintts" name="model"  class="form-control">
     </select>

     </div>
      <br />

</div>

 <div id="div13" class="hide">
<br />
 <div>
            <label for='wheels'><b>wheels:</b> <span>(*)</span></label>
      <select  name='wheels' id='wheels'>
       <option value=0>c</option>
        <option value=2>v</option>
         <option value=3>v</option>


      </select>
      </div>
      <br />
      <div>

               <label for='wheels'><b>colors:</b> <span>(*)</span></label>
      <select  name='colors' id='colors'>
       <option value=0>a</option>
        <option value=2>d</option>
         <option value=3>c</option>


      </select>
     </div>
      <br />

</div>
</div>

this is how i make hide and show works 这就是我制作隐藏和显示作品的方式

<script>

function catego() {     

    if ($('#catego').val() == 1 ) {
        $('#div12').removeClass('hide');
        $('#div13').addClass('hide');

    }

    if ($('#catego').val() == 2) {
        $('#div12').addClass('hide');
        $('#div13').removeClass('hide');
    }



}
$('#catego').on('change', catego);

$( document ).ready(function() {
    catego();
});
</script>

this is my ajax call 这是我的ajax电话

 <script>
function showHint(str) {
    if (str.length == 0) { 
        document.getElementById("txtHintts").innerHTML = "";
        return;
    } else {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("txtHintts").innerHTML = xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET", "/gethint?q=" + str, true);
        xmlhttp.send();
    }
}
</script>

Remove the inline calling of 删除的内联调用

<select  name='cars' id='cars' onchange="showHint(this.value)">

Call this method in the js after the show/hide is done. 显示/隐藏完成后,在js中调用此方法。 Also, keep all the js events inside document.ready. 另外,将所有js事件保留在document.ready中。

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

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