简体   繁体   English

来自ajax修改文件的JavaScript在主页上不起作用

[英]JavaScript from ajax modified file doesn't work in main page

I have a problems with a form and ajax behavior when I populate my dynamic div using JavaScript. 使用JavaScript填充动态div时,表单和ajax行为存在问题。

I have a drop box which contains different time periods and it triggers JavaScript function which modifies the container div based on provided value. 我有一个包含不同时间段的保管箱,它会触发JavaScript函数,该JavaScript函数根据提供的值修改容器div。

The problem is that the POST behavior of the form and all JavaScript elements doesn't work in the main file. 问题在于表单和所有JavaScript元素的POST行为在主文件中不起作用。

Here is the code. 这是代码。 I simplified it to emphasize the problem. 我简化了它以强调问题。 this is the drop Box: 这是投递箱:

//jQuery for datePicker comes here

<select id="QSelector" method="post" onchange="populateDiv(this.value)">
   <option value="0">All Values</option>
   <option value="1">Sets for 1</option>
   <option value="2">Sets for 2</option>
   <option value="3">Sets for 3</option>
   <option value="4">Sets for 4</option>
</select>

<div id="OUtputDiv" ></div>

here is the javascript function: 这是javascript函数:

          function populateDiv(id){
          var xmlhttp;
          if(window.XMLHttpRequest){//safari, chrome, opera, ffox
             xmlhttp=new XMLHttpRequest();
          }
          else{//IE
             xmlhttp=ActiveXObject("Microft.XMLHTTP");
          }

          xmlhttp.onreadystatechange=function(){
          if(xmlhttp.readyState==4 && xmlhttp.status==200){
             document.getElementById("OUtputDiv").innerHTML = xmlhttp.responseText;
           }
         }
         xmlhttp.open("GET", "../scripts/supportFile.php?ID=" + id, true);
         xmlhttp.send();
   }

And here is the content of the support file. 这是支持文件的内容。 don't mind the id variable it does some logic (ant it works), but I removed it because it's irrelevant to a given problem. 不要介意id变量会执行一些逻辑(它能起作用),但是我删除了它,因为它与给定的问题无关。

  <?php

     $Id= $_GET['ID'];
      echo "<script> alert('it works'); </script>";
      echo "<form name='AddForm' class='AddForm' method='post' >";
      echo "<span > Date: &nbsp; </span> <input type='textbox' id='datepicker' name='datepicker' > &nbsp;";
      echo "<input type='submit' name='SubBtn' value='Save' >";
      echo "</form>";

      if(isset($_POST['SubBtn']))
      {
          echo $_POST["datepicker"];
      }
   ?>

Nether JavaScript or JQuery does not work from the main file. 不管是JavaScript还是JQuery都不能从主文件运行。 And form does not post anything if I press the save button. 如果我按保存按钮,则表单不会发布任何内容。 Can anyone suggest how can I make it responsive in the main file where I call ajax function from? 谁能建议我如何在调用ajax函数的主文件中做出响应?

Avoid using IE5/6 support, use jQuery instead: 避免使用IE5 / 6支持,而是使用jQuery:

The problem is that you put all code in the same page, it will not work in that way, you need to make 2 files, 1 for post 1 for result 问题是您将所有代码放在同一页中,因此无法正常工作,您需要制作2个文件,其中1个用于发布结果1个

Good: 好:

test_post.php test_post.php

<?php
      $Id = $_POST['id'];
      if(!empty($Id)){

          $result = "
                        <script> alert('it works'); </script>
                        <form name='AddForm' class='AddForm' method='post' >
                        <span > Date: &nbsp; </span> <input type='textbox' id='datepicker' name='datepicker' > &nbsp;
                        <input type='submit' name='SubBtn' value='Save' >
                        </form>
                    ";

          echo json_encode(array('status' => 'success', 'result' => $result));
      }
?>

test.php test.php的

<select id="QSelector">
   <option value="0">All Values</option>
   <option value="1">Sets for 1</option>
   <option value="2">Sets for 2</option>
   <option value="3">Sets for 3</option>
   <option value="4">Sets for 4</option>
</select>

<div id="OUtputDiv" ></div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
    var $ =jQuery.noConflict(); 
    $(document).ready(function(){

        $(document).on( "change", '#QSelector',function(e) {
            e.preventDefault();
            var id = $(this).val();
            $.ajax({
                    type: "POST",
                    url:"<?php echo './test_post.php'; ?>",    
                    data: {id: id},
                    dataType: 'json',
                    success: function(data){
                        if(data.status == 'success'){
                            $('#OUtputDiv').html(data.result);
                        }
                    }
            });
        });
    });
</script>

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

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