【问题标题】:store id value into hidden field将 id 值存储到隐藏字段中
【发布时间】:2021-06-11 12:47:30
【问题描述】:

我这里有 2 个标签

<ul id="myTab" role="tablist">
      <li class="nav-item active">
          <a href="#doc" role="tab" aria-controls="queue" aria-selected="true" aria-expanded="true" >Documentation</a>
      </li>
      <li class="nav-item">
          <a href="#comment" role="tab" aria-controls="filePDFDoc" aria-selected="false" aria-expanded="false">Comments</a>
      </li>
</ul>

单击任何选项卡时,我想将其 ID 保存在隐藏字段中。 稍后我将在该 ID 上添加“IF 条件”。

【问题讨论】:

标签: asp.net asp.net-mvc asp.net-core


【解决方案1】:

单击任何选项卡时,我想将其 ID 保存在隐藏字段中。稍后我将在该 ID 上添加“IF 条件”。

您可以参考以下示例代码来实现需求。

<ul id="myTab" role="tablist">
    <li class="nav-item active">
        <a href="#doc" role="tab" aria-controls="queue" aria-selected="true" aria-expanded="true">Documentation</a>
    </li>
    <li class="nav-item">
        <a href="#comment" role="tab" aria-controls="filePDFDoc" aria-selected="false" aria-expanded="false">Comments</a>
    </li>
</ul>
<input id="hf_activetab" type="hidden" value="" />

JS代码

<script>
    $(function () {
        $("a[role='tab']").click(function () {
            var txt = $(this).text();
            //make sure you set the id attr for tabs, and get the id attr of current clicked tab 
            //you can use following code snippet
            console.log($(this).attr("id"));

            if (txt == "Documentation") {
                //store the data in hidden field
                $("#hf_activetab").val("doc");
            } else {
                $("#hf_activetab").val("comment");
            }

            console.log($("#hf_activetab").val());
        })
    })
</script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-27
    • 1970-01-01
    相关资源
    最近更新 更多