我想在我的文本框旁边显示错误消息,而不是在onkeyup事件中提醒

的HTML

<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;
    }
});`

最佳答案

在输入旁边创建一个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>

10-04 22:08
查看更多