2010-03-18 70 views
0

我正在使用一個JQuery選項卡式菜單,它包含不同類型的表單,當我選擇位於不同類型的選項卡下的不同表單並提交表單時,選項卡將跳轉到默認選項卡,而不是表單所在的當前選項卡JQuery選項卡導航菜單和PHP表單問題?

我在想如何解決這個問題,這樣當表單被提交時,當前標籤仍然被選中,是JQuery還是PHP問題?

這裏是JQuery。

$(document).ready(function() { 

    //When page loads... 
    $(".form-content").hide(); //Hide all content 
    var firstMenu = $("#home-menu ul li:first"); 
    firstMenu.show(); 
    firstMenu.find("a").addClass("selected-link"); //Activate first tab 
    $(".form-content:first").show(); //Show first tab content 

    //On Click Event 
    $("#home-menu ul li").click(function() { 

     $("#home-menu ul li a").removeClass("selected-link"); //Remove any "selected-link" class 
     $(this).find("a").addClass("selected-link"); //Add "selected-link" class to selected tab 
     $(".form-content").hide(); //Hide all tab content 

     var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the selected-link tab + content 
     $(activeTab).fadeIn(); //Fade in the selected-link ID content 
     return false; 
    }); 

}); 

回答

0

是,當你提交一個表單頁面刷新的原因,而是將數據發送到服務器,和javascript保持在頁面加載簡單的起始位置!

如果你想達到你想要的,你必須做一個AJAX調用!

你也可以使用一些插件這樣

http://www.mikage.to/jquery/jquery_history.html

恕我直言,你需要從頭開始AJAX!

所以請閱讀本教程!

http://php4every1.com/tutorials/jquery-ajax-tutorial/

基本上你的形式是這樣的:

<form methot="POST" action="yourphppage.php" name="myform" id="myform"> 
     <input type="text" value="" id="username" /> 

你需要做這樣的事情來傳遞您的數據到AJAX!

$('#myform').submit(function() { 

var username = $('username').val(); 

$.ajax({ 
type: "POST",  
url: "yourphppage.php",  
data: "action=postdata&username=" + username,  
success: function(data){ 
    // at this point if all is ok you 
//receive data back and you can do other stuff! 
// data is what you keep back from server (php)  
    $('#your_div_that_hold_ajax_responce').html(data); 

}, error: function(data){ 
alert('something goes wrong!');  
} 
}); 
}); 

你的PHP代碼如下所示:

if (isset($_POST['action'])) { 

switch($_POST['action']) { 

case 'postdata': 
echo $_POST['username']; 
break; 

} 
} 
+0

是否有可能與jQuery? – SlAcKeR 2010-03-19 00:10:51

+0

當然有可能讓我有時間向你發佈一些其他的代碼! – 2010-03-19 00:13:14