Jquery UI 选项卡和 spring mvc

标签 jquery spring-mvc jquery-ui-tabs

我现在对 javascript 不太了解,但在网上发现了 jquery ui 选项卡并想尝试一下。

它们开箱即用,但是当我在一个选项卡中有一个将信息发送到后端的表单时。响应(从 spring mvc Controller 发送回)无法返回到同一位置。结果是进入没有选项卡的页面,或者将其自身定位在选项卡内容的末尾。

我需要做什么吗?

$(function() {
    $( "#tabs" ).tabs({
        ajaxOptions: {
            error: function( xhr, status, index, anchor ) {
                $( anchor.hash ).html(
                    "Couldn't load this tab. We'll try to fix this as soon as possible. " +
                    "If this wouldn't be a demo." );
            }
        }

    });
});
<ul>
     <li><a href="/home/"><span>Home</span></a></li>
     <li><a href="/admin/"><span>Admin</span></a></li>
     <li><a href="/support/"><span>Support</span></a></li>
 </ul>

最佳答案

您确定使用ajax在选项卡中发送表单吗?如果您不使用ajax而只是发送表单,则浏览器将返回的页面替换当前页面是正常的。您需要 ajax 将更新的内容放置在当前选项卡中。

你必须以这种方式发送表单(例如使用jquery,我没有测试过,如果有任何拼写错误,抱歉):

<form id="my_form">
     ....
</form>

<div id="response_container">
    here I want to see the response
</div>

以及发送和接收响应的 JavaScript:

//catch the submit event in the form
$("#my_form").submit(function(){
   //when the form is going to be submitted it is sent as an ajax request
   //so the answer can be placed in the current page
   $.ajax({
       type: "POST",
       url: $("#my_form").attr('action'),
       data: $("#my_form").serializeArray(),
       dataType: 'html',
       success: function(msg){
            //place the returned html response whenever you want
            $("#response_container").html(msg);
        }
   });

   //return false to avoid the default submit() behaiour can take place
   return false;
});

关于Jquery UI 选项卡和 spring mvc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4899631/

相关文章:

java - MVC 设计 - 我可以在 Controller 类中进行域模型验证吗?

spring-mvc - 在 Spring Boot 中使用 StandardPasswordEncoder

jquery-ui - 停止第一个 jquery ui 选项卡使用 ajax 预加载内容

jquery - 如何阻止 jQuery UI 选项卡内的 SWF 重新加载

jquery - 互联网浏览器 :Hover style getting stuck on jQuery sortable element

javascript - 来自变量的地理服务器层

json - 使用 Jackson JSON View 而不注释原始 bean 类

javascript - 如果只有一个项目,jCarousel 什么也不显示

javascript - 如何计算 html 元素的类更改的次数?

jquery - 我怎样才能把它放到一个函数中?