表单提交后更改提交按钮的文本?

时间:2014-07-03 11:22:36

标签: javascript php jquery input submit

我有一个电子邮件注册表单,并希望在发送表单时,提交按钮从“提交”更改为“感谢”。一直在挖掘,但一直未能找到解决方案。

 <form id="signup" action="<?=$_SERVER['PHP_SELF']; ?>" method="get">
      <fieldset>

            <span id="response">
                <? require_once('inc/store-address.php'); if($_GET['submit']){ echo storeAddress(); } ?>
              </span>
          </label>
          <input type="text" name="email" id="email" placeholder="your e-mail address" />
          <input type="submit" name="submit" value="submit" class="btn" alt="submit" />

          <div id="no-spam"><!--We'll never spam or give this address away--></div>
      </fieldset>
    </form>      

我在这里使用MailChimp的简单订阅:http://apidocs.mailchimp.com/downloads/mcapi-simple-subscribe-jquery.zip

感谢您的帮助!!

3 个答案:

答案 0 :(得分:1)

您可以使用简写if语句来更改结果,您不需要JavaScript来执行此操作。除非你需要什么?

<input type="submit" name="submit" value="<?=($_GET['submit'])? "Thanks" : "Submit"?>" class="btn" alt="submit" />

答案 1 :(得分:0)

试试这个

$("#signup").submit(function(e)
{
    var postData = $(this).serializeArray();
    var formURL = $(this).attr("action");
    $.ajax(
    {
        url : formURL,
        type: "POST",
        data : postData,
        success:function(data, textStatus, jqXHR)
        {
            $("input[type='submit']").val("Thanks");
        },
        error: function(jqXHR, textStatus, errorThrown)
        {
            //if fails     
        }
    });
    e.preventDefault(); //STOP default action
    e.unbind(); //unbind. to stop multiple form submit.
});

$("#signup").submit();

答案 2 :(得分:0)

首先,添加onsubmit属性。

<form id="signup" action="<?=$_SERVER['PHP_SELF']; ?>" method="get" onsubmit="sayThanks()">

在提交按钮中添加id

<input type="submit" name="submit" id="submitButton" value="submit" class="btn" alt="submit" />

最后,声明sayThanks()函数:

function sayThanks(){
    document.getElementById("submitButton").value = "Thanks!";
}