简体   繁体   English

Jquery表单提交只能工作一次

[英]Jquery form submit works only once

I am making a webpage showing the frequency of searches for some searchterms in a table. 我正在制作一个网页,显示在表格中搜索某些搜索字符的频率。 Above that table are 2 radio buttons that enables the user to select if he wants to see distinct search results or all results. 该表上方有2个单选按钮,用户可以选择是否要查看不同的搜索结果或所有结果。

When using this javascript: 使用此javascript时:

$(function() {
    $('.distinct_radio').change(function() {
        console.log("submit distinct_radio");
        this.form.submit();
    });
});

It works fine. 它工作正常。 I can toggle over and over again, switching table values correctly. 我可以一遍又一遍地切换,正确切换表值。 However, if I use this javascript: 但是,如果我使用这个javascript:

$(function() {
    $('.distinct_radio').change(function() {
        console.log("submit distinct_radio");
        $('form#searchTermForm').submit();
    });
});

The form is defined as 表格定义为

<form id="searchTermForm" action="/searchterms.php" method="POST">
    <div class="configDiv"> 
        <input name="distinct" id="radio-choice-v-2a" class="distinct_radio" value="false" <?php if (strcmp($distinct, "false") == 0) {echo "checked=\"checked\"";}?> type="radio">
        <label for="radio-choice-v-2a">Show total number of searches.</label>
        <input name="distinct" id="radio-choice-v-2b" class="distinct_radio" value="true" <?php if (strcmp($distinct, "true") == 0) {echo "checked=\"checked\"";}?> type="radio">
        <label for="radio-choice-v-2b">Show number of different people</label>
    </div>
</form>

Some relevant php: 一些相关的PHP:

<?php
    if(isset($_POST['distinct'])){
        $distinct   =   $_POST['distinct'];
    } else {
        $distinct   =   "false";
    }
?>

Javascript that I am using: 我正在使用的Javascript:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="js/custom.js"></script>
<script type="text/javascript" src="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.js"></script>

Updating this to jquery mobile 1.3.1 does not resolve the issue. 将此更新到jquery mobile 1.3.1无法解决问题。

It works fine the first time clicking the radio button. 第一次单击单选按钮时,它工作正常。 After that, no response at all. 在那之后,根本没有回应。 If I manually refresh the page, it works for one time again. 如果我手动刷新页面,它再次工作一次。

With the first script, the message "submit distinct_radio" is printed on the console, and that the console is cleared by the submission itself. 使用第一个脚本,控制台上会打印“submit distinct_radio”消息,并且提交本身会清除控制台。

With the second script, the message "submit distinct_radio" is printed on the console, but the console is not cleared. 使用第二个脚本,控制台上会打印“submit distinct_radio”消息,但不会清除控制台。 It prints that message, and the POST request being done. 它打印该消息,并完成POST请求。 However, the POST request is really done and the web page is updated correctly. 但是,POST请求确实已完成,并且网页已正确更新。 Clicking again does not result in any message printed at the console, nor any request done. 再次单击不会导致在控制台上打印任何消息,也不会执行任何请求。

With the second script, if I change the path after "action" to some non-existent one, a console message is printed every click, accompanied by a new attempt to do a POST request to the non-existing location. 使用第二个脚本,如果我将“action”之后的路径更改为某个不存在的路径,则每次单击都会打印一个控制台消息,同时新的尝试向不存在的位置发出POST请求。

I face this problem in FireFox, Chrome and Opera. 我在FireFox,Chrome和Opera中遇到了这个问题。

Next to curiosity, I want to use this to submit the form when the user clicks on the table header to sort. 好奇心旁边,我想用这个来提交表单,当用户点击表格标题进行排序时。 From there, I cannot use "this.form.submit();" 从那里,我不能使用“this.form.submit();”

How can this behavior be explained? 如何解释这种行为?

Edit: 编辑:

Thanks to @peddle (See answer below): Seems some issue in Jquery Mobile. 感谢@peddle(参见下面的答案):似乎在Jquery Mobile中存在一些问题。 Deleting the reference to jquery.mobile-1.3.0.min.js makes it work over and over again (hence very ugly). 删除对jquery.mobile-1.3.0.min.js的引用会使它一次又一次地工作(因此非常难看)。 Javascript references are like suggested at http://jquerymobile.com/blog/2013/04/10/announcing-jquery-mobile-1-3-1/ , so jqm 1.3.1 and jquery 1.9.1 are meant to cooperate. Javascript引用类似于http://jquerymobile.com/blog/2013/04/10/announcing-jquery-mobile-1-3-1/ ,因此jqm 1.3.1和jquery 1.9.1是合作的。

This is not an answer but required more space than a comment, have you tried the following to see if it makes any difference: 这不是一个答案,但需要比评论更多的空间,您是否尝试过以下内容以查看它是否有任何区别:

$(function(){
  $('.distinct_radio').change(function() {
     console.log("submit distinct_radio");
    $('form#searchTermForm').get(0).submit();
  });
});

The former of your examples is using pure JavaScript to submit the form and the latter is using jQuery's version of submit. 您的示例的前者使用纯JavaScript来提交表单,后者使用jQuery的提交版本。 The above uses jQuery to find the form, but should use the JavaScript submit method. 以上使用jQuery查找表单,但应使用JavaScript提交方法。 The above should test the following: 以上应测试以下内容:

  1. If the above works without problem then your issue is with jQuery's submit. 如果上面的工作没有问题,那么你的问题是jQuery的提交。
  2. If the above fails, but produces a undefined object error, then jQuery can't find the form. 如果上面的失败,但产生了一个undefined object错误,那么jQuery找不到该表单。
  3. If the above fails but without error you have something very odd happening, or the JavaScript is not being evaluated for some reason. 如果上述操作失败但没有错误,则会发生非常奇怪的事情,或者由于某种原因未对JavaScript进行评估。

update 更新

Another thing to try: 另一件事要尝试:

$(function(){
  $('.distinct_radio').change(function() {
     console.log("submit distinct_radio");
    $('form#searchTermForm').trigger('submit');
  });
});

Now I would expect the above to have the same problem as $().submit() but you never know. 现在我希望上面的问题与$().submit()有同样的问题,但你永远不会知道。 Another thing that is possibly causing an issue is that you don't seem to have a submit button in your form markup. 另一个可能导致问题的事情是您的表单标记中似乎没有提交按钮。 Whilst logically this shouldn't have any bearing on the situation, if you read through the jQuery .submit page they have this quote: 虽然逻辑上这不应该对情况有任何影响,如果你通读jQuery .submit页面,他们有这样的引用:

http://api.jquery.com/submit/ http://api.jquery.com/submit/

Depending on the browser, the Enter key may only cause a form submission if the form has exactly one text field, or only when there is a submit button present. 根据浏览器的不同,如果表单只有一个文本字段,或者只有存在提交按钮时,Enter键才会导致表单提交。 The interface should not rely on a particular behavior for this key unless the issue is forced by observing the keypress event for presses of the Enter key. 除非通过观察按下Enter键的按键事件来强制解决问题,否则界面不应该依赖于此键的特定行为。

Whilst the above is only talking about using the enter key, it is possible that browsers/jQuery have other oddities/differences laying about when submit buttons are missing.. I am reaching however. 虽然以上只是讨论使用回车键,但浏览器/ jQuery可能存在其他奇怪/差异,当提交按钮丢失时...我正在接触。

Beyond the above it is possible there is some interference between jQuery and jQuery Mobile (your specific version), or it could be a bug in jQuery 1.9.1 (I can't say I've used this much in production). 除了上述内容之外,jQuery和jQuery Mobile(您的特定版本)之间可能存在一些干扰,或者它可能是jQuery 1.9.1中的一个错误(我不能说我在生产中使用过这么多)。 If you investigate the source of the $().trigger() method, which is used when using $().submit() , you'll see it's doing a lot of "magic" — which is why it's probably better to use the pure JavaScript method when you can get your hands on it. 如果你研究使用$().submit()时使用的$().trigger()方法的来源,你会发现它正在做很多“魔术” - 这就是为什么它可能更好用当您可以获得它时,纯JavaScript方法。

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

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