更改表单提交上的选项卡

时间:2017-09-08 17:21:48

标签: jquery ajax twitter-bootstrap

我使用bootstrap在我的html文件中创建了以下标签



<li><a href="#section1" data-toggle="tab">Section 1</a></li>
<li><a href="#section2" data-toggle="tab">Section 2</a></li>
<li><a href="#section3" data-toggle="tab">Section 3</a></li>
<li><a href="#section4" data-toggle="tab">Section 4</a></li>
&#13;
&#13;
&#13;

每个标签都有一个表单。所有表单数据均由AJAX提交。我想切换表单提交标签。例如,当用户在section1中提交表单时,我希望表单由ajax提交,然后显示第2部分中的下一个表单。

我使用以下脚本提交表单数据

&#13;
&#13;
$("#section1Form").submit(function(e) {

  var url = "storeDetails.php"; 
  $.ajax({
    type: "POST",
    url: url,
    data: $(this).serialize(), 
    success: function(data){
       alert(data);
    }
  });

  e.preventDefault();
  $(this).trigger('reset');
});
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

您可以添加表单的成功
   $('a[href="#section2"]').trigger('click');如下所示

$("#section1Form").submit(function(e) {

var url = "storeDetails.php"; 
$.ajax({
type: "POST",
url: url,
data: $(this).serialize(), 
success: function(data){
   alert(data);
 $('a[href="#section2"]').trigger('click');    }
});

e.preventDefault();
$(this).trigger('reset');

});

答案 1 :(得分:0)

由于ajax是异步的,并且在完成后会执行你的“成功”回调,你应该只需要在alert语句之后添加你的jquery:

  $.ajax({
    type: "POST",
    url: url,
    data: $(this).serialize(), 
    success: function(data){
       alert(data);
       // put your jquery tab display logic here
    }
  });

答案 2 :(得分:0)

$('a[href="#section2"]').trigger('click');// after form submit as shown in example

$("#section1Form").submit(function(e) {

    var url = "storeDetails.php";
    $.ajax({
        type: "POST",
        url: url,
        data: $(this).serialize(),
        success: function(data) {
            console.log(data);
            $('a[href="#section2"]').trigger('click');
        }
    });

    e.preventDefault();
    $(this).trigger('reset');
});
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.12.4.js"></script>
  <script src="//code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<div id="tabs">
  <ul>
    <li><a href="#section1"><span>One</span></a></li>
    <li><a href="#section2"><span>Two</span></a></li>
    <li><a href="#section3"><span>Three</span></a></li>
  </ul>
  <div id="section1">
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
  </div>
  <div id="section2">
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
  </div>
  <div id="section3">
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
  </div>
</div>