如何简洁地(临时)将按钮转换为选项卡切换器?

huangapple go评论52阅读模式
英文:

How can I succinctly (and temporarily) turn a button into a tab toggler?

问题

这段代码位于jQuery事件监听器内部。$(this) 是一个 <form> 元素。在提交时,提交按钮会变成绿色,显示 "User added" 文本,然后在八秒后恢复为常规的提交按钮。你想在按钮仍然是绿色的情况下点击它时能够激活 "users-table" 标签。

首先,你尝试了以下代码:

.attr('onclick', '$("#users-table").tab("show")')

它在某种程度上起作用,但标签显示在已显示的标签之上,导航标签也没有改变。

然后,你尝试添加了一个新的事件监听器:

$(document).ready(function () {
    $('#add-new-user').on('click', '.btn-success', function () {
        $('[href="#users-table"]').tab('show');
    });
});

这段代码有效,但不够简洁。

你如何能够实现你的目标? 你希望在无需手动禁用 "add-new-user" 标签并激活 "users-table" 标签按钮的情况下做到这一点。这似乎与 Stack Overflow 上的 "类似" 问题不符。

英文:

Look at this code. It's inside a jQuery event listener. $(this) is a &lt;form&gt; element

            const submitButton = $(this).find(&#39;[type=submit]&#39;);

            submitButton.removeClass(&#39;btn-primary&#39;)
                .addClass(&#39;btn-success&#39;)
                .attr(&#39;value&#39;, &#39;User added!&#39;)
                .attr(&#39;type&#39;, &#39;button&#39;);

            setTimeout(function () {
                submitButton.removeClass(&#39;btn-success&#39;)
                    .addClass(&#39;btn-primary&#39;)
                    .attr(&#39;value&#39;, &#39;Submit&#39;)
                    .attr(&#39;type&#39;, &#39;submit&#39;)
            }, 9000);

On submit, the submit button becomes green, displays a "User added" text and then, after eight seconds, becomes a regular submit button again. I want it to be able to activate the "users-table" tab on click while it's still green

First, I tried this

.attr(&#39;onclick&#39;, &#39;$(\&#39;#users-table\&#39;).tab(\&#39;show\&#39;)&#39;)

It kind of worked, but the tab was displayed on top of the already displayed tab and the nav tabs didn't change either

如何简洁地(临时)将按钮转换为选项卡切换器?

I then tried adding a new event listener

$(document).ready(function () {
    $(&#39;#add-new-user&#39;).on(&#39;click&#39;, &#39;.btn-success&#39;, function () {
        $(&#39;[href=&quot;#users-table&quot;]&#39;).tab(&#39;show&#39;);
    });
});

It worked, but it's not succinct

How can I achieve my objective? I would like to avoid manually deactivating the add-new-user tab and activating the users-table tab button (and then reversing it on timeout)

It seems "similar" questions here on SO don't answer my question

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

    &lt;!DOCTYPE html&gt;
    &lt;html lang=&quot;en&quot;&gt;
    &lt;head&gt;
        &lt;meta charset=&quot;UTF-8&quot;&gt;
        &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1, shrink-to-fit=no&quot;&gt;
        &lt;link rel=&quot;stylesheet&quot; href=&quot;https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css&quot;
              integrity=&quot;sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T&quot; crossorigin=&quot;anonymous&quot;&gt;
        &lt;title&gt;Admin Page&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
    &lt;div class=&quot;container&quot;&gt;
        &lt;div class=&quot;row mb-2&quot;&gt;
            &lt;ul class=&quot;nav nav-tabs&quot;&gt;
                &lt;li class=&quot;nav-item&quot;&gt;
                    &lt;a class=&quot;nav-link active&quot; data-toggle=&quot;tab&quot; href=&quot;#users-table&quot;&gt;All Users&lt;/a&gt;
                &lt;/li&gt;
                &lt;li class=&quot;nav-item&quot;&gt;
                    &lt;a class=&quot;nav-link&quot; data-toggle=&quot;tab&quot; href=&quot;#add-new-user&quot;&gt;Add New User&lt;/a&gt;
                &lt;/li&gt;
            &lt;/ul&gt;
       &lt;/div&gt;
            &lt;div class=&quot;row tab-content&quot;&gt;
            &lt;div id=&quot;users-table&quot; class=&quot;tab-pane container active&quot;&gt;
            &lt;table class=&quot;table table-hover text-center&quot;&gt;
                      &lt;thead&gt;
                            &lt;tr&gt;
                                &lt;th scope=&quot;col&quot;&gt;Name&lt;/th&gt;
                                &lt;th scope=&quot;col&quot;&gt;Last name&lt;/th&gt;
                                &lt;th scope=&quot;col&quot;&gt;Department&lt;/th&gt;
                            &lt;/tr&gt;
                      &lt;/thead&gt;
                      &lt;tbody&gt;
                            &lt;tr&gt;
                                &lt;td&gt;John&lt;/td&gt;
                                &lt;td&gt;Doe&lt;/td&gt;
                                &lt;td&gt;IT&lt;/td&gt;
                            &lt;/tr&gt;
                            &lt;tr&gt;
                                &lt;td&gt;Jane&lt;/td&gt;
                                &lt;td&gt;Doe&lt;/td&gt;
                                &lt;td&gt;HR&lt;/td&gt;
                            &lt;/tr&gt;
                     &lt;/tbody&gt;
              &lt;/table&gt;
            &lt;/div&gt;
           &lt;div id=&quot;add-new-user&quot; class=&quot;tab-pane container&quot;&gt;
              &lt;h3&gt;Fill in the forms&lt;/h3&gt;
                              &lt;form&gt;
                                  &lt;div class=&quot;form-group&quot;&gt;
                                      &lt;label for=&quot;name&quot;&gt;Name: &lt;/label&gt;
                                      &lt;input id=&quot;name&quot; name=&quot;name&quot; type=&quot;text&quot; class=&quot;form-control&quot; required /&gt;
                                  &lt;/div&gt;

                                  &lt;div class=&quot;form-group&quot;&gt;
                                      &lt;label for=&quot;last-name&quot;&gt;Last name: &lt;/label&gt;
                                      &lt;input id=&quot;last-name&quot; name=&quot;last-name&quot; type=&quot;text&quot; class=&quot;form-control&quot; required /&gt;
                                  &lt;/div&gt;

                                  &lt;div class=&quot;form-group&quot;&gt;
                                      &lt;label for=&quot;department&quot;&gt;Department: &lt;/label&gt;
                                      &lt;select id=&quot;department&quot; name=&quot;department&quot; class=&quot;form-control&quot;&gt;
                                          &lt;option value=&quot;IT&quot;&gt;IT&lt;/option&gt;
                                          &lt;option value=&quot;HR&quot;&gt;HR&lt;/option&gt;
                                          &lt;option value=&quot;accounting&quot;&gt;Accounting&lt;/option&gt;
                                      &lt;/select&gt;
                                  &lt;/div&gt;

                                  &lt;input type=&quot;submit&quot; class=&quot;btn btn-primary d-flex ml-auto&quot; value=&quot;Submit&quot; /&gt;
                              &lt;/form&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
    &lt;/div&gt;
    &lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/3.6.3/jquery.min.js&quot;&gt;&lt;/script&gt;
    &lt;script src=&quot;https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js&quot;&gt;&lt;/script&gt;
    &lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js&quot;&gt;&lt;/script&gt;
    &lt;script&gt;
$(document).ready(function () {
$(&#39;form&#39;).on(&#39;submit&#39;, async function (event) {
    event.preventDefault();
    $(&#39;tbody&#39;).append(
    `&lt;tr&gt;
         &lt;td&gt;
             ${$(this).find(&#39;[name=name]&#39;).val()}
         &lt;/td&gt;
         &lt;td&gt;
             ${$(this).find(&#39;[name=last-name]&#39;).val()}
         &lt;/td&gt;
         &lt;td&gt;
             ${$(this).find(&#39;[name=department]&#39;).val()}
         &lt;/td&gt;
     &lt;/tr&gt;`
    );
            const submitButton = $(this).find(&#39;[type=submit]&#39;);

            submitButton.removeClass(&#39;btn-primary&#39;)
                .addClass(&#39;btn-success&#39;)
                .attr(&#39;value&#39;, &#39;User added!&#39;)
                .attr(&#39;type&#39;, &#39;button&#39;);

            setTimeout(function () {
                submitButton.removeClass(&#39;btn-success&#39;)
                    .addClass(&#39;btn-primary&#39;)
                    .attr(&#39;value&#39;, &#39;Submit&#39;)
                    .attr(&#39;type&#39;, &#39;submit&#39;)
            }, 8000);
});
});
    &lt;/script&gt;
    &lt;/body&gt;
    &lt;/html&gt;

<!-- end snippet -->

答案1

得分: 3

你的选择器显示用户表格的方式是错误的,应该是$('[href="#users-table"]'),而不是$("#users-table")

我建议避免使用attr来设置处理程序,这会强制你必须转义内容并使你的代码难以阅读。只需使用.click(() => $('[href="#users-table"]').tab('show'));.off('click') 来删除处理程序。

英文:

Your selector to show the users-table was wrong, it should be $(&#39;[href=&quot;#users-table&quot;]&#39;) instead of $(&quot;#users-table&quot;)

I would avoid setting handlers with attr. That forces you to have to escape content and makes your code hard to read. Just use .click(() =&gt; $(&#39;[href=&quot;#users-table&quot;]&#39;).tab(&#39;show&#39;)); and .off(&#39;click&#39;) to remove the handlers.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(document).ready(function() {
  $(&#39;form&#39;).on(&#39;submit&#39;, async function(event) {
    event.preventDefault();
    $(&#39;tbody&#39;).append(
      `&lt;tr&gt;
         &lt;td&gt;
             ${$(this).find(&#39;[name=name]&#39;).val()}
         &lt;/td&gt;
         &lt;td&gt;
             ${$(this).find(&#39;[name=last-name]&#39;).val()}
         &lt;/td&gt;
         &lt;td&gt;
             ${$(this).find(&#39;[name=department]&#39;).val()}
         &lt;/td&gt;
     &lt;/tr&gt;`
    );
    const submitButton = $(this).find(&#39;[type=submit]&#39;);

    submitButton.removeClass(&#39;btn-primary&#39;)
      .addClass(&#39;btn-success&#39;)
      .attr(&#39;value&#39;, &#39;User added!&#39;)
      .attr(&#39;type&#39;, &#39;button&#39;)
      .click(() =&gt; $(&#39;[href=&quot;#users-table&quot;]&#39;).tab(&#39;show&#39;));

    setTimeout(function() {
      submitButton.removeClass(&#39;btn-success&#39;)
        .addClass(&#39;btn-primary&#39;)
        .attr(&#39;value&#39;, &#39;Submit&#39;)
        .attr(&#39;type&#39;, &#39;submit&#39;)
        .off(&#39;click&#39;);

    }, 8000);
  });
});

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;

&lt;head&gt;
  &lt;meta charset=&quot;UTF-8&quot;&gt;
  &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1, shrink-to-fit=no&quot;&gt;
  &lt;link rel=&quot;stylesheet&quot; href=&quot;https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css&quot; integrity=&quot;sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T&quot; crossorigin=&quot;anonymous&quot;&gt;
  &lt;title&gt;Admin Page&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
  &lt;div class=&quot;container&quot;&gt;
    &lt;div class=&quot;row mb-2&quot;&gt;
      &lt;ul class=&quot;nav nav-tabs&quot;&gt;
        &lt;li class=&quot;nav-item&quot;&gt;
          &lt;a class=&quot;nav-link active&quot; data-toggle=&quot;tab&quot; href=&quot;#users-table&quot;&gt;All Users&lt;/a&gt;
        &lt;/li&gt;
        &lt;li class=&quot;nav-item&quot;&gt;
          &lt;a class=&quot;nav-link&quot; data-toggle=&quot;tab&quot; href=&quot;#add-new-user&quot;&gt;Add New User&lt;/a&gt;
        &lt;/li&gt;
      &lt;/ul&gt;
    &lt;/div&gt;
    &lt;div class=&quot;row tab-content&quot;&gt;
      &lt;div id=&quot;users-table&quot; class=&quot;tab-pane container active&quot;&gt;
        &lt;table class=&quot;table table-hover text-center&quot;&gt;
          &lt;thead&gt;
            &lt;tr&gt;
              &lt;th scope=&quot;col&quot;&gt;Name&lt;/th&gt;
              &lt;th scope=&quot;col&quot;&gt;Last name&lt;/th&gt;
              &lt;th scope=&quot;col&quot;&gt;Department&lt;/th&gt;
            &lt;/tr&gt;
          &lt;/thead&gt;
          &lt;tbody&gt;
            &lt;tr&gt;
              &lt;td&gt;John&lt;/td&gt;
              &lt;td&gt;Doe&lt;/td&gt;
              &lt;td&gt;IT&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
              &lt;td&gt;Jane&lt;/td&gt;
              &lt;td&gt;Doe&lt;/td&gt;
              &lt;td&gt;HR&lt;/td&gt;
            &lt;/tr&gt;
          &lt;/tbody&gt;
        &lt;/table&gt;
      &lt;/div&gt;
      &lt;div id=&quot;add-new-user&quot; class=&quot;tab-pane container&quot;&gt;
        &lt;h3&gt;Fill in the forms&lt;/h3&gt;
        &lt;form&gt;
          &lt;div class=&quot;form-group&quot;&gt;
            &lt;label for=&quot;name&quot;&gt;Name: &lt;/label&gt;
            &lt;input id=&quot;name&quot; name=&quot;name&quot; type=&quot;text&quot; class=&quot;form-control&quot; required /&gt;
          &lt;/div&gt;

          &lt;div class=&quot;form-group&quot;&gt;
            &lt;label for=&quot;last-name&quot;&gt;Last name: &lt;/label&gt;
            &lt;input id=&quot;last-name&quot; name=&quot;last-name&quot; type=&quot;text&quot; class=&quot;form-control&quot; required /&gt;
          &lt;/div&gt;

          &lt;div class=&quot;form-group&quot;&gt;
            &lt;label for=&quot;department&quot;&gt;Department: &lt;/label&gt;
            &lt;select id=&quot;department&quot; name=&quot;department&quot; class=&quot;form-control&quot;&gt;
              &lt;option value=&quot;IT&quot;&gt;IT&lt;/option&gt;
              &lt;option value=&quot;HR&quot;&gt;HR&lt;/option&gt;
              &lt;option value=&quot;accounting&quot;&gt;Accounting&lt;/option&gt;
            &lt;/select&gt;
          &lt;/div&gt;

          &lt;input type=&quot;submit&quot; class=&quot;btn btn-primary d-flex ml-auto&quot; value=&quot;Submit&quot; /&gt;
        &lt;/form&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;/div&gt;
  &lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/3.6.3/jquery.min.js&quot;&gt;&lt;/script&gt;
  &lt;script src=&quot;https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js&quot;&gt;&lt;/script&gt;
  &lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js&quot;&gt;&lt;/script&gt;
&lt;/body&gt;

&lt;/html&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月14日 00:01:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76007527.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定