【问题标题】:Jquery form submit not working when query string is present in URL当 URL 中存在查询字符串时,Jquery 表单提交不起作用
【发布时间】:2015-11-09 17:54:41
【问题描述】:

测试场景:

  1. 在文本框中输入“hello”,您应该会在页面中看到“hello”
  2. 现在点击“测试”链接(它只是一个指向自身的链接,带有查询字符串&test=1
  3. 现在在文本框中输入“world”,您可以看到它不再写在页面中了。

为什么会这样?

您可以在.php 页面上测试此页面:

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" />
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
</head>
<body>

    <form id="search_form" method="post">
        <label for="search">Search:</label>
        <input type="search" id="search" />
    </form>

    <script type="text/javascript">

        $('#search_form').submit(function (event) {         
            window.location.href = '<?=$_SERVER['PHP_SELF'].'?s='?>' + $('#search').val();
            event.preventDefault();
        });

    </script>   

    <br /><br />

    <?php $s = filter_input(INPUT_GET, 's'); ?>

    Query string: <?=$s?>

    <br /><br />

    <a href="<?=$_SERVER['PHP_SELF']?>?s=<?=$s?>&test=1">test</a>

</body>
</html>

演示网址:(问题解决后删除)

【问题讨论】:

  • 请详细说明第二次发生了什么
  • @Diego 我希望表单能够正常工作(例如,打印用户在 texbox 中输入的内容),即使在单击“测试”链接之后也是如此。
  • @M.Doye 谢谢。我从未使用过或见过它。会检查的。
  • @IMB,效果很好!!
  • 另外,由于您使用的是 JQM,您可能需要将 data-ajax="false" 添加到您的 A 测试标签中。

标签: javascript php jquery html jquery-mobile


【解决方案1】:

由于您使用的是 Jquery Mobile,所有链接默认通过 AJAX 执行。这会混淆你的这个脚本。我建议在链接中隐藏它。

&lt;a href="&lt;?=$_SERVER['PHP_SELF']?&gt;?s=&lt;?=$s?&gt;&amp;test=1" data-ajax="false"&gt;test&lt;/a&gt;

这将允许链接被视为正常链接,并将在没有 AJAX 的情况下执行 URL。

【讨论】:

猜你喜欢
  • 2010-11-10
  • 2018-02-24
  • 1970-01-01
  • 2012-02-02
  • 1970-01-01
  • 1970-01-01
  • 2018-07-26
  • 2012-12-13
相关资源
最近更新 更多