-4

我有一个带有输入文本框和按钮的表单。当我单击一个按钮时,我想根据输入文本框的值转到不同的页面。例如:

<form name="frm1" id="frm1" action="page.php">
    <input type="text" name="txt_name" id="txt_name" value="simple text" />
    <input type="button" Onclick="redirect_to('page2.php/?id=8&input=this.txt_name.value')" value="Save" />
</form>

我怎样才能得到这个值?这可以在不使用函数的情况下完成吗?

4

4 回答 4

3

好吧,我会推荐一个功能,原因如下:

  1. 更干净的代码。
  2. 如果您有多个按钮,则更好的方法。
  3. 更少的代码。
  4. 更好的可管理性。

将此添加到您的按钮

onclick="redirect('mytextbox.value');";

将此添加到您的标记中<head>(只需几行代码):

<script type="text/javascript">
    function redirect(value){
        window.location="page.php/?id=8&input="+value.ToString();
        }
</script>  
于 2012-06-30T16:09:45.673 回答
2

你为什么不这样做呢?

<form name="frm1" id="frm1" action="page2.php">
    <input type="hidden" value="8" name="id" />
    <input type="text" name="txt_name" id="txt_name" value="simple text" />
    <input type="submit" value="Save" />
</form>
于 2012-06-30T16:13:54.933 回答
1

如果你有类似的东西:

<form>
  <input type="text" name="formelem" />
  <input type="button" />
</form>

你可以把按钮:

onclick="redirect_to('page.php/?id=8&input='+this.form.formelem.value)"

您在按钮上,因此“this”将是按钮,您需要获取可以从中获取输入的表单

另一种方式(如果您不需要其他目的的表格)就是放:

<input type="text" id="formelem" />
<input type="button" onclick="redirect_to('page.php/?id=8&input='+document.getElementById('formelem').value)" />
于 2012-06-30T16:10:54.787 回答
0
onclick="window.location.href = 'page.php/?id=8&input=' +this.value.toString()"
于 2012-06-30T16:03:09.113 回答