繁体   English   中英

根据从选择标记中选择的选项来选择表单操作

[英]Select a form action depending on the option chosen from a select tag

我对此很陌生,因此这可能不是解决此问题的最佳方法。 基本上,我试图在提交表单时运行三个.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>

您是否进行了调试以查看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;
}

应该

function onSubmitForm(){

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

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

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

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM