自动选择输入html

时间:2019-02-05 11:08:24

标签: javascript jquery html html5

我的表单很简单,我想在输入中添加自动选择,

这是我的解决方法。

表单HTML

 <div class="video large-8 columns large-push-4">
                        <iframe width="100%"  id="videoframe" src="videoframe.html" autoplay>   
                      </iframe>
                </div>

        <div class="form">
                                <span class="form-title">Wypełnij formularz, nasz doradca oddzwoni do Ciebie!</span>
                                <div class="centered">
                                    <input name="client_name" id="client_name" type="text"  placeholder="Imię" class="clientName medium small-12 select_input" />
        </div>
    </div>

这是videoframe.js。

      $(document).ready(function(){
       document.getElementById('playervideo').addEventListener('timeupdate', function (e) {
        if(stateplayer=="przywitanie" && !$("#playervideo").hasClass("autoselectname") ) {
            if(document.getElementById('playervideo').currentTime >  5) {
                console.log("Zaselektuj pole imie");
                $("#playervideo").addClass("autoselectname");
           //  var inputSelect =document.getElementById('client_name');
                var inputSelect = document.getElementById('client_name');
                inputSelect.focus()
            }
        }
        //console.log("timeupdate: " + document.getElementById('playervideo').currentTime);

    })

   });

这是播放器videoframe.html视频标记

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Wideo iframe</title>
    <script src="https://code.jquery.com/jquery-3.3.1.js"></script>
    <link rel="stylesheet" href="css/style.css">

</head>

<body>
    <div id="videowrapper">
        <video  id="playervideo" playsinline controls muted>
        </video>
        <!-- <canvas id="videocanvas" width="1024" height="576"></canvas> -->
        <div class="fader"></div>
        <span id="customPlayer">
                <img src="./img/replay.png">
            </span>
            <span id="customPlayerMute">
                <img src="./img/wyciszony.png">
            </span>
            <span id="customPlayerVoice" class="customPlayerVoice not_clicked"  style="display: none">
                    <img src="./img/glosny.png">
            </span>
    </div>
</body>
<script src="jslibs/howler.min.js"></script>
<script src="https://code.createjs.com/1.0.0/createjs.min.js"></script>
<script src="js/videoframe.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mobile-detect/1.4.3/mobile-detect.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/mobile-detect@1.4.3/mobile-detect.min.js"></script>

</html>

但是不幸的是,我遇到了以下错误

jquery-3.3.1.js:3827 Uncaught TypeError: Cannot read property 'select' of null
    at HTMLDocument.<anonymous> (videoframe.js:256)
    at mightThrow (jquery-3.3.1.js:3534)
    at process (jquery-3.3.1.js:3602)

console.log(inputSelect)返回此null

我的代码在做什么错?

2 个答案:

答案 0 :(得分:0)

没有足够的声誉来发表评论。您可以尝试将defer标记添加到脚本中。 <script src="js/videoframe.js" type="text/javascript" defer></script>

答案 1 :(得分:0)

如果看到粘贴的调用堆栈,则错误与inputSelect元素无关。您在这里出错:videoframe.js:256

我的猜测是,您的代码未达到尝试访问inputSelect元素的地步。尝试放置调试器,并将代码固定在videoframe.js的第256行。

您提供的代码段中没有.select,因此,我认为,当您尝试进行something.select()时,结果就是null