簡體   English   中英

第二個文本框僅允許較小數量的Jquery

[英]Second text box to allow only smaller number Jquery

我有兩個文本框。
用戶在此框中輸入數字。 我正在尋找一種解決方案,以使第一個框號必須大於或等於第二個框號,反之亦然。
舉例來說,如果用戶在第一個框中輸入70,則第二個框中應僅接受70或更少的數字。 有沒有辦法實現這一目標。 這在aspx頁面中。

您可以在以下情況下使用focusout

 $("#txt2") .focusout(function() { var num = $('#txt1').val(); if ($('#txt2').val() > num) alert('false post'); //Your other code or message else alert('true post'); }) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="number" id='txt1' /> <input type="number" id='txt2' /> 

您可以同時獲取數字並進行比較

 function validateInput() { let getFirstVal = document.getElementById('firstInput').value; let getSecondVal = document.getElementById('secondInput').value; if (isNaN(getFirstVal)) { console.log('Enter only number'); document.getElementById('secondInput').setAttribute('disabled', true) } else if (getFirstVal === '') { document.getElementById('secondInput').setAttribute('disabled', true) } else if (parseInt(getSecondVal, 10) > parseInt(getFirstVal, 10)) { document.getElementById('secondInput').value = ''; console.log('Enter lower number'); } else { document.getElementById('secondInput').removeAttribute('disabled') } } 
 <div> <input type='text' id='firstInput' onkeyup='validateInput()'> <input disabled type='text' id='secondInput' onkeyup='validateInput()'> </div> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM