2012-10-23 188 views
0

我對此很新,所以這可能不是解決此問題的最佳方法。基本上,我試圖在提交表單時運行三個.php文件中的一個。我運行的.php文件應取決於用戶從ID =「periodDisplay」的選擇字段中選擇的值。真的很感謝一些幫助。根據從選擇標記中選擇的選項選擇表單操作

<script> 

    function onSubmitForm(){ 

    if(document.getElementById("periodDisplayed").selectedIndex == 'This Week') 
    { 
     return document.selectDisplay.action ="thisWeek.php"; 
    } 

    else if(document.getElementById("periodDisplayed").selectedIndex == 'This Month') 
    { 
     return document.selectDisplay.action ="thisMonth.php"; 
    } 

    else if(document.getElementById("periodDisplayed").selectedIndex == 'All Workouts') 
    { 
      return document.selectDisplay.action ="allWorkouts.php"; 
    } 

    else 
    { 
     return document.selectDisplay.action = "index.html"; 
    } 
return true; 
    } 
</script> 




<form id="selectDisplay" onsubmit="return onSubmitForm();"> 
    I want to see 
    <select id="unitDisplayed"> 
     <option>Distance</option> 
     <option>Time</option> 
    </select> 
    for 
    <select id="periodDisplayed"> 
     <option>This Week</option> 
     <option>This Month</option> 
     <option>All Workouts</option> 
    </select> 

    <input type="submit"></input> 
</form> 
+0

不是這應該是的document.getElementById( 「periodDisplayed」)。selectedIndex.value – Satya

回答

1

您是否調試瞭解selectedIndex返回的內容?它返回一個INTEGER,而不是所選選項的值/文本。

爲什麼這麼複雜。將值設置爲您想要轉到的頁面並引用它。所有的代碼都被縮減爲一行。

HTML

<select id="periodDisplayed"> 
    <option value="thisWeek.php">This Week</option> 
    <option value="thisMonth.php">This Month</option> 
    <option value="all.php">All Workouts</option> 
</select> 

的JavaScript

function onSubmitForm() { 
    document.selectDisplay.action = document.getElementById("periodDisplayed").value; 
    return true; 
} 
+0

真棒,th是完美的作品。非常感謝! – user1767115

0

應該

function onSubmitForm(){ 

if(document.getElementById("periodDisplayed").value == 'This Week') 
{ 

    document.selectDisplay.action ="thisWeek.php"; 
    return true; 
} 
.... 

document.selectDisplay.action = "index.html"; 
return true; 

}