簡體   English   中英

提交表單時使用javascript打開表單標簽

[英]Open tab in form with javascript on submit

我有一個非常簡單的表單示例,該表單被拆分為多個選項卡,然后將其發布到表單上以使用php進行驗證和處理,簡化后的代碼如下(無需驗證和處理步驟):

<style>
/* Style the list */
ul.tab {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    border: 1px solid #ccc;
    background-color: #f1f1f1;
}

/* Float the list items side by side */
ul.tab li {float: left;}

/* Style the links inside the list items */
ul.tab li a {
    color: black;
    display: inline-block;
    font-size: 15px;
    font-weight: lighter;
    padding: 14px 16px;
    text-align: center;
    text-decoration: none;
    transition: all 0.3s ease 0s;
}

/* Change background color of links on hover */
ul.tab li a:hover {background-color: #ddd;}

/* Create an active/current tablink class */
ul.tab li a:focus, .active {background-color: #ccc;}

/* Style the tab content */
.tabcontent {
    display: none;
    padding: 60px 10px;
    border: 1px solid #ccc;
    border-top: none;
    margin-bottom: 20px;
}
</style>

<ul class="tab">
<li><a href="#" class="tablinks" onclick="openTab(event, 'tab_1')">tab 1</a></li>
<li><a href="#" class="tablinks" onclick="openTab(event, 'tab_2')">tab 2</a></li>
</ul>

<!-- FORM Starts -->
<form method="POST" action="">
<div id="tab_1" class="tabcontent">
Tab One Content
</div>

<div id="tab_2" class="tabcontent">
Tab Two Content
</div>

<input type="submit" name="save_settings_button" value="Save Settings" />
</form>

<script>
function openTab(evt, tabName) {
    var i, tabcontent, tablinks;
    tabcontent = document.getElementsByClassName("tabcontent");
    for (i = 0; i < tabcontent.length; i++) {
        tabcontent[i].style.display = "none";
    }
    tablinks = document.getElementsByClassName("tablinks");
    for (i = 0; i < tabcontent.length; i++) {
        tablinks[i].classList.remove("active");
    }
    document.getElementById(tabName).style.display = "block";
    evt.currentTarget.classList.add("active");
}

var mybtn = document.getElementsByClassName("tablinks")[0];
mybtn.click();
</script>

這有助於將大表格分解為多個部分,但是如果用戶在選項卡2上並按下提交按鈕,則會將其帶回到選項卡1,這不是很好的用戶體驗!

有什么方法可以使用javascript來識別提交表單時哪個標簽,然后在重新加載頁面后在該標簽上重新加載頁面?

由於您在提交表單后使用php處理所有內容,因此在頁面刷新后知道活動選項卡的唯一方法是在表單中添加自定義字段,並在更改選項卡時對其進行更新。

所以在你的HTML

<input type="hidden" id="active_tab" name="active_tab" value="tab_1" />

然后,您應該編輯openTab函數以在調用此輸入時對其進行更新。

document.getElementById("active_tab").value = tabName;

現在在php中,當加載頁面時,您將添加一個新變量來捕獲活動選項卡,並將活動類更新到您的選項卡。

$active_tab = $_POST['active_tab'];
$active_tab1 = "";
$active_tab2 = "";
if ($active_tab ==="tab_1") $active_tab1 = "active";
if ($active_tab ==="tab_2") $active_tab2 = "active";

然后最后更新選項卡以使用活動類。

<div id="tab_1" class="tabcontent <?php echo $active_tab1; ?>">...</div>
div id="tab_2" class="tabcontent <?php echo $active_tab2; ?>">...</div>

但是,這並不是最佳方法,為了獲得更好的用戶體驗,我將更改所有提交給ajax調用的表單並處理外部php文件中的所有內容。

我將檢查active類的存在,以確定當前哪個標簽處於活動狀態。 例如,諸如tablink.classList.contains('active')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM