提交按钮的Onclick元素不调用JS函数

时间:2015-03-02 15:35:31

标签: javascript php html onclick

嗨我熟悉使用onsubmit和onclick,因为我之前使用过它们,但由于某种原因,这次它拒绝工作。我查看了我的旧代码并尝试了所有内容以使其完全相同但仍然无法运行。

function verifypass()
{
var password = "Testpass";
var string = document.getElementByID("verifybadge");
if(string!=password) {
alert("Wrong password!");
window.location="about:blank";
}
}
<form name="receivingform" method="post">
</br></br>
<b>Please Enter Password to Verify Closure of Ticket: </b> </br>
<input type="text" size="15" maxlength="20" id="verifybadge"> </br>
<input class="button_text" type="submit" value="Delete Closed Rows"      onclick="verifypass();">
</form>

1 个答案:

答案 0 :(得分:3)

伙计,document.getElementById 顺便说一下,让我们清理一下这段代码:

HTML:

<form name="receivingform" method="post">
    <br/><br/>
    <b>Please Enter Password to Verify Closure of Ticket: </b> <br/>
    <input type="text" size="15" maxlength="20" id="verifybadge" /> <br/>
    <input class="button_text" type="submit" value="Delete Closed Rows" onclick="verifypass()" />
</form>


使用Javascript:

function verifypass() {
    var password = "Testpass";
    var string = document.getElementById("verifybadge").value;
    if(string !== password) {
        alert("Wrong password!");
        window.location="about:blank";
    }
}

此代码有效 (注意@John建议的.value)

这里有一个片段:

function verifypass() {
  var password = "Testpass";
  var string = document.getElementById("verifybadge").value;
  if(string !== password) {
    alert("Wrong password!");
    //window.location="about:blank";
  } else alert("yay");
}
<form name="receivingform" method="post">
  <br/><br/>
  <b>Please Enter Password to Verify Closure of Ticket: </b> <br/>
  <input type="text" size="15" maxlength="20" id="verifybadge" /> <br/>
  <input class="button_text" type="button" value="Delete Closed Rows" onclick="verifypass()" />
</form>