jquery - 使用 Ajax 提交有关下拉列表更改的表单

标签 jquery ajax

我有以下内容,它调用另一个文件并根据下拉值吐出输出。它工作正常,但我似乎无法在没有提交按钮的情况下使其正常工作。这是可行的,只是它将自身重定向到具有正确输出的 process.php 。这段代码的重点是在空 div 中显示输出(output)。

$(document).ready(function() {
   $('#dropdown').change( function() {
       $('#myform').submit();
       $.ajax({ // create an AJAX call...
           data: $(this).serialize(), // get the form data
           type: $(this).attr('method'), // GET or POST
           url: $(this).attr('action'), // the file to call
           success: function(response) { // on success..
               $('#output').html(response); // update the DIV
           }
       });
       return false; // cancel original event to prevent form submitting
    });
});


<form id="myform" method=POST action="process.php">
<select id="dropdown" name="dropdown">
    <option value="QU">QU</option>
    <option value="QF">QF</option>
    <option value="QC">QC</option>
</select>
</form>
<div id="output"></div>

最佳答案

如果你想使用ajax那么就不要提交表单,同样在下拉更改事件中this是下拉元素而不是表单。

$(document).ready(function() {
   $('#dropdown').change( function() {
       $.ajax({ // create an AJAX call...
           data: $('#myform').serialize(), // serialize the form
           type: $('#myform').attr('method'), // GET or POST from the form
           url: $('#myform').attr('action'), // the file to call from the form
           success: function(response) { // on success..
               $('#output').html(response); // update the DIV
           }
       });
    });
});

关于jquery - 使用 Ajax 提交有关下拉列表更改的表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13957993/

相关文章:

javascript - 将数组从 Node 服务器发送到js脚本

mysql - 西里尔文、AJAX 和数据库

javascript - 鼠标悬停时垂直滚动 div

jquery - 如何在ajax成功中解析JSON对象

javascript - prevAll() 在更复杂的 DOM 中不起作用

php - 简单的 PHP MySQL 分页(显示更多按钮,带参数)

jquery - 当我给 <tbody> 标签时无法追加行

javascript - jquery将textarea更改为基于select的读写

c# - 是否有一种标准方法可以将 .NET 字符串编码为 JavaScript 字符串以便在 MS Ajax 中使用?

javascript - 数据表操作按钮未被禁用