6

HTML 是:

<input name="submit"
       type="submit"
       class="button"
       value="Click Here"
       tabindex="13"
       onclick="return ValidateForm();" />

ValidateForm() 函数具有所有常用的表单验证代码。我无法运行的另一个功能(除了它本身它工作正常..example

<input name="submit"
       type="submit"
       class="button"
       value="Click Here"
       tabindex="13"
       onclick="disDelay(this);" />

我尝试将它们都放在 onclick ...示例之后

<input name="submit"
       type="submit"
       class="button"
       value="Click Here"
       tabindex="13"
       onclick="return ValidateForm(); disDelay(this);" />

我还尝试将代码放在同一个函数中,但没有成功。

函数 disDelay() 是

function disDelay(obj){
    obj.setAttribute('disabled','disabled');
    setTimeout(function(){obj.removeAttribute('disabled')},10000);
}

它被用作延迟,以防止表单从多次点击中获得重复提交。现在延迟为 10 秒,仅用于测试目的。我需要验证和延迟才能一起工作。

4

3 回答 3

4

返回第一个函数的值会终止单击处理程序。从本质上讲,这就是您尝试合并时所做的事情:

<input name="submit" type="submit" class="button" 
       value="Click Here" tabindex="13" 
       onclick="return submit_Click(this);" />

<script type="text/javascript">
    function submit_Click(sender) {
        return ValidateForm(); 
        disDelay(sender); // !!! This call is unreachable !!!
    }
</script>

这是纠正它的一个简单选项:

<input name="submit" type="submit" class="button" 
       value="Click Here" tabindex="13" 
       onclick="return submit_Click(this);" />

<script type="text/javascript">
    function submit_Click(sender) {
        var r = ValidateForm(); 
        disDelay(sender); // It seems like you would only want to call this
                          // function if the form is validate, so there should
                          // probably be an if-statement surrounding it. However,
                          // I'll leave that up to you.
        return r;
    }
</script>
于 2012-08-06T20:41:14.380 回答
2

You almost had it, just reverse the order of the function calls.

<input name="submit" type="submit" class="button" value="Click Here" tabindex="13" onclick="disDelay(this); return ValidateForm(); " />

The return from ValidateForm is going to cause the rest of the code to be unreachable so it must be last.

于 2012-08-06T20:42:17.740 回答
0

In your onclick string the disDelay(this) is not getting called because you return the response from ValidateForm(), so the second statement never executes.

If you absolutely must use the onclick attribute inside the html, try:

<input name="submit" type="submit" class="button" value="Click Here" tabindex="13" onclick="disDelay(); return ValidateForm();" />
于 2012-08-06T20:43:53.917 回答