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 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,