【问题标题】:set form fields when buttons are pressed按下按钮时设置表单域
【发布时间】:2014-11-08 01:53:09
【问题描述】:

我有一个用 PHP 扩展保存的 html 页面。它包含一个带有两个字段 home_page_text 和 aff_page_text 的输入表单。它还包含两个按钮。

<!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>
</head>
 <body bgcolor="#EEE8AA">
  <form name="Form1" id="Form1" action="post_message.php" method="post">
  <input type="text" size="80" maxlength="400" 
  name="home_page_text" id="home_page_text"
  value="" style="background-color:#EFD381" />

  <input type="text" size="80" maxlength="400" 
  name="aff_page_text" id="aff_page_text"
  value="" style="background-color:#EFD381" />

  <input class="stand_alone_button_left_12em" type="button" value="Default The Message" 
  name="default_home" 
  onclick="return OnButton1();" />

  <input class="stand_alone_button_left_12em" type="button" value="Default The Message" 
  name="default_aff" 
  onclick="return OnButton2();" />

每当按下相应的按钮时,我希望将这两个字段中的每一个设置为不同的默认值。

所以如果我按下按钮 default_home 我希望字段 home_page_text 设置为“这是主页默认值”。相应地,如果我按下按钮 default_aff 我希望字段 aff_page_text 设置为“这是 AFF PAGE 默认值”。

这些值只能是默认值。用户可以按原样接受文本,或编辑默认文本。当按下第三个按钮(type = submit)时,页面将被提交,导致 Post 调用类似“post_message.php”的页面。

我怎样才能做到这一点?

【问题讨论】:

    标签: php html forms button


    【解决方案1】:

    您是否为此项目安装了 jQuery 库?使用 jQuery 更容易完成,否则您将不得不使用纯 JavaScript。

    几乎,当 default_home 按钮被按下时,jQuery 会将 home_page_text 设置为您想要的任何内容,如果按下另一个按钮,则相同。

    否则,您可以使用 Ajax 甚至 PHP 来完成此操作。使用 PHP,每次按下不同的按钮时都需要刷新整个页面。

    让我知道你想如何解决这个问题:PHP、jQuery 或 Ajax(jQuery 是最有效的)

    【讨论】:

    • 我过去使用过 jquery.cycle.all.js。我这样称呼它:
    • 我刚刚检查了 HostGator(我的托管服务器)并意识到它们支持 jquery。非常感谢您的帮助。
    【解决方案2】:

    即使使用纯 javascript 也可以实现这一点。如果按下 default_home 按钮,在 OnButton1() 方法中您可以修改输入的值,如下所示,

    document.getElementById("home_page_textotc").value = "This is the HOME PAGE default value";
    

    同理,在 OnButton2() 方法中,

    document.getElementById("aff_page_textThis is the AFF PAGE default value").value = "This is the HOME PAGE default value";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-26
      • 1970-01-01
      • 1970-01-01
      • 2011-06-10
      • 1970-01-01
      • 2021-02-22
      • 2019-09-06
      相关资源
      最近更新 更多