我正在制作一个网页,其中显示了一些搜索词的搜索频率表。在该表格上方有两个单选按钮,用户可以选择查看独特搜索结果或所有结果。
当使用以下JavaScript时:
$(function() {
$('.distinct_radio').change(function() {
console.log("submit distinct_radio");
this.form.submit();
});
});
它运行良好。 我可以一遍又一遍地切换,正确地切换表格值。 但是,如果我使用此JavaScript:
$(function() {
$('.distinct_radio').change(function() {
console.log("submit distinct_radio");
$('form#searchTermForm').submit();
});
});
表单被定义为:
<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>
一些相关的PHP:
<?php
if(isset($_POST['distinct'])){
$distinct = $_POST['distinct'];
} else {
$distinct = "false";
}
?>
我正在使用的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>
将它更新到jquery mobile 1.3.1并不能解决问题。第一次单击单选按钮时有效,之后就没有响应了。如果我手动刷新页面,它又能再次工作一次。使用第一个脚本,控制台会打印出“submit distinct_radio”消息,并且提交本身会清除控制台。使用第二个脚本,控制台会打印出“submit distinct_radio”消息,但不会清除控制台。它会打印出该消息和正在执行的POST请求。然而,POST请求确实已经完成,网页也已经正确更新。再次单击不会导致在控制台上打印任何消息,也不会执行任何请求。使用第二个脚本,如果我更改“action”后面的路径为不存在的路径,则每次单击都会打印出控制台消息,并伴随着尝试向不存在的位置进行POST请求。我在FireFox、Chrome和Opera中遇到了这个问题。除了好奇心外,我还想在用户单击表头进行排序时使用它来提交表单。从那里开始,我就不能使用“this.form.submit();”。如何解释这种行为?编辑:感谢@peddle(请参见下面的答案):似乎是Jquery Mobile的问题。删除对jquery.mobile-1.3.0.min.js的引用可以让它一次又一次地工作(因此非常丑陋)。Javascript引用建议使用http://jquerymobile.com/blog/2013/04/10/announcing-jquery-mobile-1-3-1/,因此jqm 1.3.1和jquery 1.9.1应该可以相互协作。
$('#searchTermForm').submit();
- Deepanshu Goyal