簡體   English   中英

由於某種原因,使用 jQuery 附加 keyup 處理程序不起作用

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

我正在嘗試為不同的操作獲取書面的 integer 值,但它不起作用。 但是另一個頁面上的類似內容可以正常工作。 我該如何解決?

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:



<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>

我該如何解決這個問題? 控制台中沒有錯誤。 有時它會顯示如下錯誤:

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

我認為您的腳本未加載或 CDN 中存在問題。 使用 Java 腳本獲得類似結果。 例如。 HTML:

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

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

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 腳本代碼:

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

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

我認為這行得通

您正在加載 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>

只使用1個版本的jquery,1.11版可能太舊了


如果您的腳本在輸入元素呈現之前運行 - 例如,如果腳本在head元素中定義,它將無法通過 id 找到input

在查詢和附加監聽器之前,您可以等待完整的文檔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