2014-09-22 38 views
0

我創建了3個使用輸入類型按鈕顯示的div,這些按鈕用於顯示和隱藏最初隱藏的div。在這些div中有用於存儲數據的表單。現在我想要做的是在提交表單中的數據後保留當前使用的div。

在表單提交後保留顯示的div

這是我的jQuery代碼。

$("#add_exams").hide(); 
    $("#exams").hide(); 
    $("#add_accts").hide(); 

    $("#view_exam").click(function(){ 
     $("#add_exams").hide(); 
     $("#exams").hide(); 
     $("#add_accts").hide(); 

     $("#exams").fadeIn(); 
    }); 

    $("#create_exam").click(function(){ 
     $("#add_exams").hide(); 
     $("#exams").hide(); 
     $("#add_accts").hide(); 

     $("#add_exams").fadeIn(); 
    }); 

    $("#manage_accts").click(function(){ 
     $("#add_exams").hide(); 
     $("#exams").hide(); 
     $("#add_accts").hide(); 

     $("#add_accts").fadeIn(); 
    }); 

//$(document).on("click", "#selectall",function(){ 
    $(document).on("submit","#submit_acct", function(){ 

     $("#add_accts").fadeIn(); 

    }); 

我的HTML是這樣

<div id="content"> 
<input type="button" id="create_exam" value="Create Exam" title="Click to create new exam." /> 
    <input type="button" id="view_exam" value="Exam" title="Click to view exam." /> 
    <input type="button" id="manage_accts" value="Accounts" title="Click to view exam." /> 
    <div id="add_accts"> 
     /*forms goes here */ 
    </div> 

    <div id="add_exams"> 
     /*forms goes here */ 
    </div> 

    <div id="exams"> 
     /*forms goes here */ 
    </div> 
</div> 

當我嘗試提交數據,股利不顯示,我試圖用一個jQuery將顯示我想要的股利,但它不工作,所有這些都是隱藏的。我只需要保留以顯示當前顯示的div,對此的任何想法?

+0

嘗試添加event.preventDefault();到您的提交js並做了阿賈克斯之後 – Roi 2014-09-22 08:15:15

+0

我想你已經錯過了一些東西 – Mayank 2014-09-22 08:54:40

回答

0

據我所知,你想保留可見div即使在後回。您可以使用隱藏字段來保留div可見性。嘗試下面的代碼片斷供參考

$("#view_exam").click(function(){ 
     $("#add_exams").hide(); 
     $("#exams").hide(); 
     $("#add_accts").hide(); 

     $("#exams").fadeIn(); 
     $("#FiddenField_For_Current_Form").val("exams"); 
    }); 

,並在頁面加載檢查值FiddenField_For_Current_Form和顯示適當的形式爲以下

$(document).ready(function(){ 
    switch($("#FiddenField_For_Current_Form").val()) 
    { 
    case "exams": 
      $("#exams").fadeIn(); 
      break; 

    default : 
      $("#add_exams,#exams,#add_accts").hide(); 
      break; 

    } 


}); 
+0

有一個問題,這個..我使用這種輸入類型,並且正如預期的那樣改變了值的點擊事件,但它仍然是相同的,我手動添加值=「考試」的輸入,它工作得很好。 – user3627265 2014-09-22 08:54:34

+0

從$(「FiddenField_For_Current_Form」)。val(「exams」); to $(「#FiddenField_For_Current_Form」)。val(「exams」); – Mayank 2014-09-22 08:56:26