0

私はこれに非常に慣れていないので、これはこの問題を解決するための最良の方法ではないかもしれません. 基本的に、フォームの送信時に 3 つの .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>
4

2 に答える 2

1

selectedIndex が何を返すかを確認するためにデバッグしましたか? 選択したオプションの値/テキストではなく、INTEGER を返します。

なぜそんなに複雑にするのですか。移動したいページに値を設定して参照します。すべてのコードが 1 行に短縮されます。

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;
}
于 2012-10-23T03:13:30.963 に答える
0

する必要があります

function onSubmitForm(){

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

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

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

}
于 2012-10-23T03:16:10.170 に答える