将 id 值存储到隐藏字段中

时间:2021-06-11 12:47:30

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

我这里有 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 条件”。

1 个答案:

答案 0 :(得分:0)

<块引用>

单击任何选项卡时,我想将其 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>
相关问题