-1

我想从

例子.php

example.php?month=year-month

我有以下脚本:

<script type=\"text/javascript\">
  function select() {
    var url = window.location.href;
    var year = document.getElementById('myyear').value;
    var month = document.getElementById('mymonth').value;
    document.my_months.action = url + '?month=' + year + '-' + month;
  }
</script>

和以下形式:

echo "<form id =\"my_months\" name= \"my_months\" method=\"post\" onsubmit=\"return select();\">
  <select name = \"myyear\" id = \"myyear\">";

  foreach (range(2012, date('Y')) as $years) {
    if ($years == date('Y')) {
      echo "<option value= \"".$years."\" selected= \"".$years."\">".$years."</option>";
    } else {
      echo "<option value= \"".$years."\">".$years."</option>";
    }
  }
  echo "</select>
        <select name = \"mymonth\"> id = \"mymonth\"";

  foreach ($myMonths as $id => $months) {
    if ($months == date('F')) {
      echo "<option value= \"".$id."\" selected= \"".$id."\">".$months."</option>";
    } else {
      echo "<option value= \"".$id."\">".$months."</option>";
    }
  }
  echo "</select>
  <input type=\"submit\" id=\"Submit_m\" name=\"Submit_m\" value=\"Ok\">
</form>";

但它不起作用。该页面将在不修改 URL 的情况下重新加载!我在这里做错了什么?

这是代码:

echo "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\" dir=\"ltr\">
<head>
  <title>title</title>
  <meta name=\"description\" content=\"\" />
  <meta name=\"robots\" content=\"index,follow\" />
  <meta name=\"keywords\" content=\"\" />
  <!--<meta http-equiv=\"refresh\" content=\"5\">-->
  <script type=\"text/javascript\" language=\"javascript\" src=\"../js/jquery-1.9.1.js\"></script>
    <script type=\"text/javascript\" language=\"javascript\" src=\"../js/modules/exporting.js\"></script>
  <script type=\"text/javascript\">
    function select() {
      var url = window.location.href;
      var year = document.getElementById('myyear').value;
      var month = document.getElementById('mymonth').value;
      document.my_months.action = url + '?month=' + year + '-' + month;
    }
  </script>
  <script type=\"text/javascript\">
    $(document).ready(
      function () {
        chart = new Highcharts.Chart({
          chart: {
            renderTo: 'container',
            defaultSeriesType: 'spline',
            zoomType: 'xy'
          },
...
        });
      }
    )
  </script>
</head>
<body>";

//here some data and arrays

echo "<form id =\"my_months\" name= \"my_months\" method=\"post\" onsubmit=\"return select();\">
  <select name = \"myyear\" id = \"myyear\">";

  foreach (range(2012, date('Y')) as $years) {
    if ($years == date('Y')) {
      echo "<option value= \"".$years."\" selected= \"".$years."\">".$years."</option>";
    } else {
      echo "<option value= \"".$years."\">".$years."</option>";
    }
  }
  echo "</select>
        <select name = \"mymonth\"> id = \"mymonth\"";

  foreach ($myMonths as $id => $months) {
    if ($months == date('F')) {
      echo "<option value= \"".$id."\" selected= \"".$id."\">".$months."</option>";
    } else {
      echo "<option value= \"".$id."\">".$months."</option>";
    }
  }
  echo "</select>
  <input type=\"submit\" id=\"Submit_m\" name=\"Submit_m\" value=\"Ok\">
</form>
<p><div id=\"container\" style=\"min-width: 200px; height: 400px; margin: 0 auto\"></div></p></body></html>";
4

2 回答 2

0
<script type=\"text/javascript\">
  function select() {
    var url = window.location.href;
    var year = document.getElementById('myyear').value;
    var month = document.getElementById('mymonth').value;
    url += '?month=' + year + '-' + month;

    // set the url in your location bar
    history.pushState({date:"true"}, "", url);
  }
</script>
于 2013-07-22T12:58:11.643 回答
0

所有建议的尝试均未成功。但是我已经将表单的方法post更改为get并且 url 更改为

example.php?myyear=myyear_value&mymonth=mymonth_value&Submit_m=OK

whwn 提交名为Submit_m的按钮

有了这些变量,我现在可以使用$_GET['myyear']$_GET['mymonth']获取值

现在我得到了预期的行为

于 2013-07-22T14:31:15.427 回答