Jquery UI 选项卡和 spring mvc

发布于 2024-10-15 15:35:03 字数 854 浏览 6 评论 0原文

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

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

有什么我需要做的吗?

$(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>

I do not really now much about javascript but found on the web the jquery ui tabs and wanted to give it a try.

They work out of the box however when I have within one tab a form that send information to the back end. The response (sent back from an spring mvc controller) is unable to come back to the same location. As a result is gone to a page without tabs or it position it self at the end of the tab content.

is there somethin I need to do?

$(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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绻影浮沉 2024-10-22 15:35:03

您确定使用 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;
});

Are you sure you are using ajax to send the form in the tab? If you don't use ajax and you just send the form it's normal that the browser puts the returned page substituting the current one. You need ajax to place updated content in the current tab.

You have to send the form in this way (for example with jquery, I haven't tested it, sorry if there is any misspelling):

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

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

and the javascript to send and receive the response:

//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;
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文