How to trigger('click') on jquery tab's currently active tab

后端 未结 3 1268
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-17 16:36

I have a tabed screen and want to trigger a click on the selected tab once the form is submitted and the return is valid. Here a part of the html:

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-17 17:03

    Try using the a[href=""] selector:

    $('#tabUL a[href="#tabProducts"]').trigger('click');
    


    I put together a jsfiddle demo to show it in action, and how to optionally hide the other tabs since often when I'm programmatically forcing tabs its to require missing mandatory information be entered on the initial tab before unlocking the other tabs...

    Edit Here is the contents of the jsfiddle:

    HTML

    This is the first tab (0)

    This is the second tab (1)

    This is the third tab (2)

    This is the fourth tab (3)


    Select the Tab and Lock the Others

    jQuery

    $(document).ready(function () {
      $('#tabs').tabs();
      $('#tabSelect').change(function () {
            //pass empty array to unlock any locked tabs
        $('#tabs').tabs({disabled: []}).find('a[href="#tab' + $(this).val() + '"]').trigger('click');
    
        if ($('#tabHide').prop('checked')) {
          //hide will be an array like [0,2,3]
          var hide = Array();
          $('#tabs li').not('.ui-tabs-active').each(function () {
            hide.push($(this).index());
          });      
          $('#tabs').tabs({disabled: hide});
        }
      });
    });
    

提交回复
热议问题