一个文本框一个按钮html中的两个不同页面 [英] one textbox one button two different pages in html

查看:380
本文介绍了一个文本框一个按钮html中的两个不同页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将文本框值分配给php变量,现在的问题是我希望一个按钮可以在两个不同的页面上工作,即如果我在文本框中输入"a"并单击按钮,则应将其重定向到"a". php"页面,如果我在文本框中输入"b",则应重定向到"b.php".

i am trying to assign a textbox value to a php variable now problem is i want one button to work for two different pages i.e if i enter in a text box 'a'and click on button it should redirect to 'a.php' page if i write in a text box 'b' it should redirect to 'b.php'.

所以一个文本框,一个按钮在两个不同的页面上.

so one textbox,one button two different pages.

代码:

<html><head>
<meta charset="UTF-8" />
<script>
function submitForm(action)
{
    document.getElementById('a').action = action;
    document.getElementById('a').submit();
}
function submitForm1(action)
{
    document.getElementById('b').action = action;
    document.getElementById('b').submit();
}

</script>
</head>
 <body >



<h3><font face="verdana" size="3"><b>Enter Text:</b></h3>

 <input type="text"  align="right" style="font-size:15pt;height:32px;"><br><br>

    <form action="b.php" name="b" id="b" method="post"> 
<form action="a.php" name="a" id="a" method="post">

 <input type="submit" onclick="submitForm('a.php')" value="TRY"  name="a">
 <input type="button" onclick="submitForm1('b.php')" value="TRY" name="b">



    </form>
    </form>


</body>
</html>

推荐答案

您可以根据输入中的文本更改onsubmit的操作.

You can change the action onsubmit based on the text inside the input.

<html>
<head>
    <script type="text/javascript">
        function onsubmit_handler(){
            var myForm = document.getElementById('myForm');
            var data = document.getElementById('data').value;
            if(data == "a")
                myForm.setAttribute('action', 'a.php');
            else if(data == "b")
                myForm.setAttribute('action', 'b.php');
            else
                myForm.setAttribute('action', 'error.php');
        }
    </script>
</head>
<body>
    <h3>Enter Text:</h3>
    <form id="myForm" method="post" onsubmit="onsubmit_handler()">
        <input type="text" id="data" name="data" value="">
        <input type="submit" value="Post">
    </form>
</body>
</html>

此处测试代码: http://jsfiddle.net/Eg9S4/

这篇关于一个文本框一个按钮html中的两个不同页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆