I need to run two onclick functions. I can't make them both work, even when they

HTML:

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

The ValidateForm () function has all the usual form validation code. Another function that I cannot run (in addition, it works fine .. example

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

I tried putting them both after onclick ... example

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

I also tried putting one code in the same function without success.

Function disDelay () -

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

It should be used as a delay so that the form does not get duplicate submissions from a few clicks. The delay is 10 seconds right now for testing purposes. I need validation and latency to work together.

+6
source share
3 answers

Returning the value of the first function completes the click handler. Essentially, this is what you do in your attempt to combine:

 <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> 

Here is one easy way to fix it:

 <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> 
+4
source

You almost had it, just reorder the function calls.

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

Returning from ValidateForm will cause the rest of the code to be inaccessible, so it should be the last.

+2
source

In your onclick line, disDelay (this) does not receive the call because you are returning a response from ValidateForm (), so the second statement never executes.

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

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

Source: https://habr.com/ru/post/922242/


All Articles