简体   繁体   English

由于某种原因,使用 jQuery 附加 keyup 处理程序不起作用

[英]Attaching a keyup handler using jQuery does not work for some reason

I'm trying to get the written integer value for different operations but it does not work.我正在尝试为不同的操作获取书面的 integer 值,但它不起作用。 But a similar thing on another page is working properly.但是另一个页面上的类似内容可以正常工作。 How can I fix it?我该如何解决?

HTML code: HTML 代码:

        <div class="row modal-body">
            <div class="col-4">
                <label for="contengencyrate">रकम</label>
            </div>
            <div class="col-7">
                <input type="number" step="any" name="amount" min="0" max="{{buktani_baki}}"  id="amount" value="">

                <!-- <input type="number" id="amount" name="amount" min="0" max="{{total.amount__sum}}" step="any" value=""> -->
            </div>
        </div>

jquery: jquery:



<script src="https://code.jquery.com/jquery-3.4.1.min.js"
    integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>

    <script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
    <script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/jquery.validate.min.js"></script>
    <script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/additional-methods.min.js"></script>
<script>


$('#amount').keyup(function () {

        var val = $('#amount').val();
        // var total = $("#total").val()
        console.log(val,890898908080);
        console.log(123456);
        // $('#percent').val(rakamTopercent(val, total))
    })


</script>

How can I solve this?我该如何解决这个问题? no error in the console.控制台中没有错误。 sometimes it shows error like:有时它会显示如下错误:

"additional-methods.min.js?_=1609744418426:4 Uncaught TypeError: Cannot read property 'addMethod' of undefined" “additional-methods.min.js?_=1609744418426:4 Uncaught TypeError: Cannot read property 'addMethod' of undefined”

I think your script is not loading or some issue in CDN.我认为您的脚本未加载或 CDN 中存在问题。 Use Java Script for a similar results.使用 Java 脚本获得类似结果。 Eg.例如。 HTML: HTML:

<div class='printchatbox' id='printchatbox'></div>

<input type='text' name='fname' class='chatinput' id='chatinput'>

CSS:{if required} CSS:{如果需要}

.printchatbox 
    {border-width:thick 10px;border-style: solid; 
    background-color:#fff;
    line-height: 2;color:#6E6A6B;font-size: 14pt;text-align:left;float: middle;
    border: 3px solid #969293;width:40%;
     height:2em;   
}

Java Script Code: Java 脚本代码:

var inputBox = document.getElementById('chatinput');

inputBox.onkeyup = function(){
    document.getElementById('printchatbox').innerHTML = inputBox.value;
}

i think this works我认为这行得通

You are loading 2 different versions of jquery :您正在加载 2 个不同版本的jquery

<script src="https://code.jquery.com/jquery-3.4.1.min.js"
    integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>

<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>

Use only 1 version of jquery, version 1.11 is probably too old只使用1个版本的jquery,1.11版可能太旧了


If your script runs before the input element is rendered - for example if the script is defined in the head element it won't find the input by id如果您的脚本在输入元素呈现之前运行 - 例如,如果脚本在head元素中定义,它将无法通过 id 找到input

You can wait for the full document to be ready before querying and attaching a listener在查询和附加监听器之前,您可以等待完整的文档ready

 <:DOCTYPE html> <html> <head> <script src="https.//code.jquery.com/jquery-3.4.1.min:js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script> <script src="https.//cdn.jsdelivr.net/jquery.validation/1.16.0/jquery.validate.min:js"></script> <script src="https.//cdn.jsdelivr.net/jquery.validation/1.16.0/additional-methods.min.js"></script> <script> // Wait for the full document to be parsed and loaded $(document).ready(function() { // And then try to find and atach a listener $("#amount").keyup(function() { var val = $("#amount");val(). // var total = $("#total").val() console,log(val; 890898908080). console;log(123456). // $('#percent'),val(rakamTopercent(val; total)) }); }). </script> </head> <body> <div class="row modal-body"> <div class="col-4"> <label for="contengencyrate">रकम</label> </div> <div class="col-7"> <input type="number" step="any" name="amount" min="0" max="{{buktani_baki}}" id="amount" value="" /> <!-- <input type="number" id="amount" name="amount" min="0" max="{{total.amount__sum}}" step="any" value=""> --> </div> </div> </body> </html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM