drl9940 2015-11-09 17:54
浏览 74
已采纳

当URL中存在查询字符串时,Jquery表单提交不起作用

Test scenario:

  1. Type "hello" in the textbox, you should see "hello" written in the page
  2. Now click the "test" link (it's just a link to itself with a query string &test=1)
  3. Now type "world" in the textbox, you can see that it doesn't get written in the page anymore.

Why is this happening?

You can test this page on a .php page:

<!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>

Demo URL: (removed after problem solved)

  • 写回答

1条回答 默认 最新

  • dongzhihong3940 2015-11-09 18:34
    关注

    Since you are using Jquery Mobile, all links are executed via AJAX by default. This will confuse this script of yours. I would suggest suppressing this in the link.

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

    This will allow the link to be treated like normal and will execute the URL without AJAX.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据