Javascript 根据从选择标记中选择的选项选择表单操作
我对这个很陌生,所以这可能不是解决这个问题的最好方法。基本上,我正在尝试在表单提交时运行三个.php文件中的一个。运行哪个.php文件应该取决于用户从id=“periodDisplay”的select字段中选择的值。非常感谢你的帮助Javascript 根据从选择标记中选择的选项选择表单操作,javascript,Javascript,我对这个很陌生,所以这可能不是解决这个问题的最好方法。基本上,我正在尝试在表单提交时运行三个.php文件中的一个。运行哪个.php文件应该取决于用户从id=“periodDisplay”的select字段中选择的值。非常感谢你的帮助 <script> function onSubmitForm(){ if(document.getElementById("periodDisplayed").selectedIndex == 'This Week') {
<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>
子表单()上的函数{
if(document.getElementById(“periodDisplayed”).selectedIndex==“本周”)
{
return document.selectDisplay.action=“thisWeek.php”;
}
else if(document.getElementById(“periodDisplayed”).selectedIndex==“This Month”)
{
return document.selectDisplay.action=“thismount.php”;
}
else if(document.getElementById(“periodDisplayed”).selectedIndex==“所有训练”)
{
return document.selectDisplay.action=“allWorkouts.php”;
}
其他的
{
return document.selectDisplay.action=“index.html”;
}
返回true;
}
我想看
距离
时间
对于
本周
这个月
所有训练
是否调试以查看selectedIndex返回的内容?它返回一个整数,而不是所选选项的值/文本
为什么要这么复杂呢。将该值设置为要转到并引用的页面。所有代码都减少到一行
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>
应该是
function onSubmitForm(){
if(document.getElementById("periodDisplayed").value == 'This Week')
{
document.selectDisplay.action ="thisWeek.php";
return true;
}
....
document.selectDisplay.action = "index.html";
return true;
}
这不应该是document.getElementById(“periodDisplayed”).selectedIndex.value吗
function onSubmitForm(){
if(document.getElementById("periodDisplayed").value == 'This Week')
{
document.selectDisplay.action ="thisWeek.php";
return true;
}
....
document.selectDisplay.action = "index.html";
return true;
}