输入文本输入后如何自动加载新页面

时间:2015-08-07 02:55:56

标签: javascript html

我正在设置一个售货亭,人们会出现并扫描包含8个字符确认号码的QR码。

在扫描了他们的二维码后,他们的确认号码被填充到一个文本字段中,我想自动重定向到另一个页面。我可以使用javascript函数来完成这个吗?

我不希望他们必须点击提交按钮或其他任何内容来执行此操作。

<form action="post.php" method="post" accept-charset="utf-8">
        <input type="text" name="confirm" id="scanner" autofocus="autofocus" value="" maxlength="8">
</form>

4 个答案:

答案 0 :(得分:2)

var scanner = document.getElementById('scanner');

function checkLength(){
  if(scanner.value.length === 8){
    window.location.assign('http://stackoverflow.com')
  }
}
<input type="text" name="confirm" id="scanner" autofocus="autofocus" value="" maxlength="8" oninput="checkLength()">

答案 1 :(得分:0)

这就是我要做的事情:

<html>
    <body>


        <form action="post.php" method="post" accept-charset="utf-8">
            <input type="text" name="confirm" id="scanner" autofocus="autofocus" value="" maxlength="8">
        </form>


        <script>
            $("#scanner").submit(function(e) {
                e.preventDefault();
                window.location.href = "http://www.google.com";
            });
        </script>


   </body>
</html>

这假设您正在使用JQuery

答案 2 :(得分:0)

您只需要使用onchange事件:
HTML:

<form action="post.php" method="post" accept-charset="utf-8">
        <input type="text" name="confirm" id="scanner" autofocus="autofocus" value="" maxlength="8" onchange="newPage();">
</form>

JavaScript:

function newPage(){
windows.location.href = www.yourlinkhere.com;
}

答案 3 :(得分:0)

&#13;
&#13;
$(document).ready(function(){
  $(document).bind('input propertychange','#scanner',function(e){
    e.preventDefault();
    window.location.assign('http://stackoverflow.com');
  });
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="post.php" method="post" accept-charset="utf-8" id="form_submit">
        <input type="text" name="confirm" id="scanner" autofocus="autofocus" value="" maxlength="8">
</form>
&#13;
&#13;
&#13;