2011-12-13 62 views
0

我有2個頁面在容器頁面中具有不同的id。第一頁有一個按鈕,如果你點擊它,那麼它會調用一個Java腳本函數。我想執行下面的一個。在java腳本函數中,condition是true,那麼只有它會使用id重定向到下一個頁面。否則不要重定向。如何做到這一點,請任何人都可以幫助我。清楚嗎?jquery手機:如何使用ID呼叫另一個頁面

CODE

< !DOCTYPE html> 

< html> 

< head> 

    < meta charset="utf-8"> 
    < meta name="viewport" content="width=device-width, initial-scale=1"> 
    < title>Single page template</title> 

     < link rel="stylesheet" href="http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.css" /> 
    < script type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.min.js"></script> 
    < script type="text/javascript" src="http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.js"></script> 

< /head> 

< body> 

< !-- BMR Screen page --> 

    < div data-role="page" id="BMR_screen"> 

    < div data-role="header" data-theme="e" id="hdrBMRScreen"> 

    < h1> BMR Screen< /h1> 
    < /div> 

    < div data-role="content" id="contentBMRScreen"> 

     < div data-role="fieldcontain"> 

      < label for="BMR_age">Age(in years):</label> 

      < input type="text" id="BMR_age" /> 

     < /div> 

     < a href="#BMR_Result" onclick="BMR_Cal()" data-role="button">submit</a> 

    < /div> 

< /div> 

< !-- End BMR Screen --> 

    < div data-role="page" id="BMR_Result"> 

     < div data-role="header" data-theme="b" > 

      < a data-role="button" data-rel="back" data-icon="back">back</a> 

      <h1>BMR Result</h1> 

     < /div> 

     < div data-role="content">  

      < p id="W_Range">Weight Range:</p> 

     < /div> 
    < /div> 

    < script type="text/javascript"> 

    var agevar; 


    $(document).ready(function() { 
    // Initialize variables 
    ageVar = $('#BMR_age');  
    }); 

    function BMR_Cal() 
    {  
    if(ageVar.val() > 0) 
    {  
     // then only it will redirect to the next page i.e, BMR_Result   
    } 

    } 
    < /script> 
    < /body> 
< /html> 
+0

你的問題有點含糊,你能發表你的代碼嗎? –

+0

我添加了代碼請看一次 – naresh

+0

它仍然有點難以遵循,你確定你正確地縮進了代碼嗎? –

回答

0

確定的問題是你分配的變量在頁面加載時(之前用戶輸入的任何東西)。

讓我們把它全部放在文檔中準備好。

$(document).ready(function() { 
// Initialize the variable by declaring it 
var ageVar ; 

function BMR_Cal() { 
//then tie it to the input element AFTER the user has inputted . 
ageVar = $('#BMR_age'); 
if(ageVar.val() > 0) { 

document.location.href="yourpagename#BMR_Result"; 
} 
}); 
+0

在這裏,我必須寫在如果塊導航到下一頁? – naresh

+0

@naresh更新了代碼。這應該工作。 Rember用* .html文件替換yourpagename,但將錨點保留在最後。 –

+0

@naresh它工作正常嗎?如果是的話,請你能標記回答的問題。 –

1

在jQuery手機中,你不應該使用ready事件。使用mobileinit事件來代替,因爲在DOM準備就緒之前,jQuery mobile的init代碼會運行。 See

此外,請考慮使用$.mobile.changePage method在JavaScript中執行頁面更改,因爲它更安全,更易於配置。