使用JavaScript更改HTML按钮文本

时间:2015-06-02 10:56:28

标签: javascript html

我正在建立一个网站来托管我的音乐直播。我做了一个部分,人们可以推荐歌曲,建议存储在SQL数据库中。我希望在提交“#34;提交建议书"”时提交此表单上的“提交”按钮。到#34;谢谢你"单击时,也可以让客户端按一次以避免泛滥。好吧,我还没有设法让这些工作在一起(并在我的标签上保留必要的属性),但我已经设法一次完成一个。这是我的代码请帮忙!

<form action="./add.php" method="POST" target="sendFormSuggest" onsubmit="return checkBeforeSubmit() ">
    <!--The form for suggestions -->
    Song: <span style="color: red">* </span>
    <input class="recom" type="text" name="song" size="50" required><br>
    Artist: <span style="color: red">* </span>
    <input class="recom" type="text" name="artist" size="50" required><br>
    YouTube Link:
    <input class="recom" type="text" name="link" size="50"><br>
    <input class="Submit" type="submit" value="Submit Recommendation" id="ButtonRecom">
</form>

<script type="text/javascript"> <!--this checks the any form for second submition -->
    var wasSubmitted = false;
    function checkBeforeSubmit() {
        if (!wasSubmitted) {
            wasSubmitted = true;
            return wasSubmitted;
            document.getElementById("ButtonRecom").value = "Thank you!";
        }
        return false;
    }
</script>

3 个答案:

答案 0 :(得分:1)

你可以尝试这样的事情: 在这里,我要将“添加客人”中的按钮文字添加到“添加家庭”     会员'

<script type="text/javascript"> 
if (!top.adminpanel) 
{         
var myPage = document.getElementByID   
('idSectionGuestListContainer');
  if (myPage)
{ 
myPage.innerHTML = myPage.innerHTML.replace('Add guest', 'Add family    
member'); 
}    
}

答案 1 :(得分:0)

您只需要切换两行:

var wasSubmitted = false;    
    function checkBeforeSubmit(){
        if(!wasSubmitted) {
            wasSubmitted = true;
            // pull this line up
            document.getElementById("ButtonRecom").value= "Thank you!";
            // push this line down
            return wasSubmitted;
        }
    return false;
    }

答案 2 :(得分:0)

建议:应在服务器端筛选一小段时间内来自同一客户端的连续请求(例如,2秒内提交5次),以便有效防止泛滥。

但是,要回答您的问题:

在更改按钮文本之前,您的代码会返回,因此您需要向下移动return。见:

    function checkBeforeSubmit(){
        if(!wasSubmitted) {
            wasSubmitted = true;
            document.getElementById("ButtonRecom").value= "Thank you!";
            return true;
        }
        return false;
    }
相关问题