最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Automatically submitting a HTML form after scanning a barcode - Stack Overflow

programmeradmin1浏览0评论

I am trying to automatically submit a form after scanning a barcode. The number I scan is always 16 digits long.

<form id="Form" action="action.php" method="post">
  <input id="here" maxlength="16" placeholder="scan..." type="text" tabindex="1" required autofocus>
  <input id="subHere" type="submit">
</form>

<script>
  $('#here').keyup(function(){
    if(this.value.length ==16){
    $('#subHere').click();
    }
  });
</script>​

The restriction of entering 16 digits is working but my form is not automatically submitting after entering the 16 digits.

I also want to clear the form after submitting, so I can scan the next barcode. Do you have any suggestions?

I am trying to automatically submit a form after scanning a barcode. The number I scan is always 16 digits long.

<form id="Form" action="action.php" method="post">
  <input id="here" maxlength="16" placeholder="scan..." type="text" tabindex="1" required autofocus>
  <input id="subHere" type="submit">
</form>

<script>
  $('#here').keyup(function(){
    if(this.value.length ==16){
    $('#subHere').click();
    }
  });
</script>​

The restriction of entering 16 digits is working but my form is not automatically submitting after entering the 16 digits.

I also want to clear the form after submitting, so I can scan the next barcode. Do you have any suggestions?

Share Improve this question edited Sep 4, 2018 at 7:54 nixfuerdiecharts asked Sep 3, 2018 at 8:00 nixfuerdiechartsnixfuerdiecharts 3836 silver badges16 bronze badges 10
  • 2 Is that all of your <form> ? It has no action and I don't see a 'click` event being set, so it won't submit anywhere. – peeebeee Commented Sep 3, 2018 at 8:07
  • Normaly Barcord machin facilitated to "press enter" after scan. Didn't you try to do with that feture? – Udara Kasun Commented Sep 3, 2018 at 8:25
  • my barcode scanner is acting like it is a keyboard, I've tried touse it this way but I do not have this feature – nixfuerdiecharts Commented Sep 3, 2018 at 8:29
  • You are currently sending your form to # which does not do anything. Have you build some code that handles the form after submitting? – Granny Commented Sep 3, 2018 at 8:38
  • 1 Working here – Fr0zenFyr Commented Sep 3, 2018 at 9:24
 |  Show 5 more ments

2 Answers 2

Reset to default 3

Thanks to Fr0zenFyr it is working now:

<form id="Form" action="./js/filehandling.js" method="post">
   <input id="here" maxlength="16" placeholder="scan..." type="text" tabindex="1" required autofocus>
   <input id="subHere" type="submit">
</form>
<script src="https://code.jquery./jquery-2.2.4.js"></script>

<script>
  $('#here').keyup(function(){
      if(this.value.length ==16){
      $('#subHere').click();
      }
  });
</script>​

after entering 16 digits, it automatically submits the form

Another way would be :

<script>
  $("#input_field").keyup(function() {
    if ($(this).val().length >= 16)
      $('#form').submit();
  })
</script>
发布评论

评论列表(0)

  1. 暂无评论