jQuery UI Tabs: Don't let user switch tabs if form input in active tab has content

余生长醉 提交于 2020-01-25 10:24:07

问题


I'm using jQuery UI Tabs, and these tabs are inside a form. Every tab contains a number of form fields, which can be filled. It looks something like this:

<form>
<tab1 href=tabcontent1> <tab2 href=tabcontent2> <tab3 href=tabcontent3>

<div tabcontent1>
<input field>
<input field>
</div>

<div tabcontent2>
<input field>
<input field>
</div>

<div tabcontent3>
<input field>
<input field>
</div>

<submit button>
</form>

Problem is, If the user for example have started filling the fields of tab1, I don't want the user to be able to fill the fields of tab2 or tab3.

So, I'm looking for a way to check if the user have started to fill the input fields of a tab, and if this is the case, hide the other tabs / disable switching to the other tabs.

If the user removes the stuff he has written, he should once again be able to switch tabs.

Thanks for your help, it's greatly appreciated!

// Jens.


回答1:


You could have used beforeActivate function of jQuery tab to do this. Since your code is not complete and hence not clear, I will just take the sample example from jQuery tabs documentation:

JS:

$(function () {
    $("#tabs").tabs({
        beforeActivate: function (event, ui) {
            var content = ui.oldPanel; // Get current content
            var input = $(content).find('input.i'); //Get input
            if (input.val()) { // Check if input has any value or whatever condition you want.
                alert('not allowed');
                return false; //Stop loading new tab if condition is satisfied.
            }
        }
    });
});

Demo: http://jsfiddle.net/lotusgodkk/GCu2D/416/



来源:https://stackoverflow.com/questions/26959344/jquery-ui-tabs-dont-let-user-switch-tabs-if-form-input-in-active-tab-has-conte

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!