我想在文本框旁边显示错误信息,而不是在onkeyup事件中发出警报

I want to show error message beside my textbox rather than alert in onkeyup event

本文关键字:事件 onkeyup 文本 信息 错误 显示      更新时间:2023-09-26

我想在文本框旁边显示错误信息,而不是在onkeyup事件中显示警报

<input type="textbox"
   id="id_part_pay" 
   value="<?php echo $listing['part_pay'];?>"
   name="part_pay" 
/>
javascript

$("#id_part_pay").keyup(function()
{
    var input = $('#id_part_pay').val();
    var v =input % 10;
    if (v!==0)
    {
      alert("Enter Percentage in multiple of 10");
    }
    if(input<20 || input>100) 
    {
      alert("Value should be between 20 - 100");
      return;
    }
});`

在input旁边创建一个span,然后将代码更改为

$(function() {
  $("#id_part_pay").next('span').hide(); //Hide Initially
  $("#id_part_pay").keyup(function() {
    var input = $(this).val();
    
    var v = input % 10;
    var span = $(this).next('span'); //Get next span element
    
    if (v !== 0) {
      span.text("Enter Percentage in multiple of 10").show(); //Set Text and Show
      return;
    }
    
    
    if (input < 20 || input > 100) {
      span.text("Value should be between 20 - 100").show();//Set Text and Show
      return;
    }
    
    span.text('').hide();//Clear Text and hide
    
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="textbox" id="id_part_pay" value="10" name="part_pay" />
<span></span>