如何使用javascript/jquery在任何文本框中查找输入或不输入的值

How to find values entered or not in any of the textboxes using javascript/jquery

本文关键字:输入 查找 文本 javascript 何使用 jquery 任何      更新时间:2023-09-26

我有三个ID的ppl1,pp2ppl3

我可以通过制作一些 javascript 函数来查找在这些文本框中输入的任何值

,如下所示
$(document).ready(function () {   
$("#ppl1").change(function () {

$(document).ready(function () {
$("#ppl2").change(function () {

$(document).ready(function () {
$("#ppl3").change(function () {

现在我需要检查是否已输入任何文本框。也就是说,在上述函数中使用一些逻辑 OR||)。

希望这可以简单地完成,但即使长时间谷歌搜索,我也无法得到确切的东西。

// Bind keyup event on the input
$('input:text').keyup(function() {
  // If value is not empty
  if ($(this).val().length != 0) {
    console.log("input with ID: " + $(this).attr('id') + " has value")
  } else {
    console.log("input with ID: " + $(this).attr('id') + " has no value")
  }
}).keyup(); // Trigger the keyup event, thus running the handler on page load
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input name="ppl1" id="ppl1" />
<input name="ppl2" id="ppl2" value='123' />
<input name="ppl3" id="ppl3" />

您可以使用.keyup()

$(function(){ // This is equal to "$(document).ready(function () {" 
    // $("[id^='ppl']") This selector works as well. 
    $("#ppl1, #ppl2, #ppl3").on('change',function(e){
        if ($(this).val().trim().length > 0)
        {
            // The input has changed, and there is something in it. 
        }
    });
});