【问题标题】:Use one submit button to enter various pages in Javascript使用一个提交按钮在 Javascript 中输入各种页面
【发布时间】:2011-03-05 23:15:19
【问题描述】:

我想用开按钮进入不同的房间。这在Javascript中怎么可能。例如,有三个房间,“厨房、卫生间和卧室”。根据我的选择,我如何使用 JS 进入这些房间中的任何一个。因此,如果我在输入框中输入“厨房”,它会带我到 kitchen.php,如果我输入厕所……同一个按钮会带我去洗手间.php 等。

这是 HTML 输入,

<form method="post">
<input style=""name="Text1" type="text"><br>
<input name="move" style="height: 23px" type="submit" value="Move">
</form>

【问题讨论】:

  • 恐怕我们需要更多的信息。也许提供有关表单提交位置、提交时会发生什么、如何确定房间等的详细信息?

标签: php javascript html input validating


【解决方案1】:

只需使用选择字段jsfiddle demo

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 

<head> 
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> 
<title>Untitled 1</title> 
<script type="text/javascript"> 
function submitForm() {
    var myform = document.getElementById('myform');
    var mytext = document.getElementById('room');
    myroom = mytext.value.toLowerCase();
    if (myroom == 'kitchen') {
        myform.action = 'kitchen.php';
        myform.submit();
    } else if (myroom == 'toilet') {
        myform.action = 'toilet.php';
        myform.submit();
    } else if (myroom == 'bedroom') {
        myform.action = 'bedroom.php';
        myform.submit();
    } else return false;
}

window.onload = function(){
     document.getElementById('move').onclick = submitForm;
}
</script> 
</head> 

<body> 
<form id="myform" name="myform" method="post"> 
    <input type="text" id="room" name="room" />
     <button id="move" name="move" style="height: 23px">Move</button> 
</form> 
</body> 
</html> 

在 php 端创建三个文件来测试是否可行,toilet.php、kitchen.php 和卧室.php,在所有三个文件中使用以下代码。确保文件名小写:

<?php
echo $_POST['room'];
?>

基本上,根据选择的选项,JavaScript 会更改表单的操作 url 并提交。如果没有选择,它将返回 false 并且不提交。 submitForm 函数通过 onclick 事件附加到移动按钮。

【讨论】:

  • 好的...我怎样才能JS选择标签...去三个单独的房间...如kitchen.php,toilet.php等
  • @Kelvin 也请投票/接受可以解决您问题的答案。它将激励人们希望将来在 StackOverflow 上为您提供帮助。
  • @kjy112 我真的不知道。即使我使用了一个全新的网页:看到这个...thirdworld.vacau.com/game/hope.php它仍然无法正常工作。另外,我需要它是输入(文本)而不是选择字段。即使它在 php.ini 中,你能帮我吗?我明天交作业:)
  • @Kelvin 我添加了 window.onload 以将 onclick 附件包装到移动按钮。看看新代码,我更改了一些内容,以便您更容易理解
  • 非常感谢。现在,最后但并非最不重要的。它关于会话。请在这里查看,请stackoverflow.com/questions/5211694/…
猜你喜欢
  • 2019-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-17
  • 1970-01-01
相关资源
最近更新 更多