0

This Code Works but when i copy and paste into it, it doesn't submit.

<script src="jquery-3.2.1.min.js"></script>

<form id="Form" action="pro_add_invoice.cfm" method="post">
    <input id="here"name="htno" type="text" value="" />&nbsp;
    <input id="subHere" type="submit" value="Submit"  />
</form>

<script>
    $('#here').keyup(function(){
        if(this.value.length ==10){
          $('#Form').submit();
        }
    });
</script>
Wicfasho
  • 311
  • 2
  • 13

3 Answers3

0

I'm assuming you just want to submit the form after ten characters are entered. You can use $().submit() instead and pass in the id of the form.

<form id="Form" action="sell.cfm" method="post">
    <input id="here"name="htno" type="text" value="" />&nbsp;
    <input id="subHere" type="submit" value="Submit"  />
</form>

<script>
    //$('#here').keyup(function(){
    //    if(this.value.length ==10){
    //      $('#Form').submit();
    //    }
    //});

    var input = document.querySelector('#here');
    input.addEventListener('keyup', checkLength);
    function checkLength(e){
        if(e.target.value.length===10){
            document.forms["Form"].submit();
        }
    }

</script>
wlh
  • 3,426
  • 1
  • 16
  • 32
0

If you want to submit the form you cannot use click event handler. That's only for click events. you need to call the submit method of the form element to submit the form.

Change your If statement to execute the following:

Vanilla JS:

document.forms.Form.submit();

or JQuery:

$('#Form').submit();

SO...

<script>
    $('#here').keyup(function(){
    if(this.value.length ==10){
      $('#Form').submit();
    }
});
</script>
Canolyb1
  • 672
  • 5
  • 17
-1

I think the problem here is this context is not belong to #here, the scope in the anonymous function (probably) belong to window.

I didn't try it yet but maybe this solve the problem, try change this to ('#here')

billyzaelani
  • 567
  • 1
  • 5
  • 18
  • Did you try it and verify that `this` was out of scope? – wlh May 22 '17 at 23:08
  • If you consist, here the [jsfidle](https://jsfiddle.net/8up4v32k/). Note: the reason I didn't verify it because i did't use JQuery, the test in jsfidle didn't use jquery but it just prove the `this` context is not belong to `#here`. – billyzaelani May 23 '17 at 03:17