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

时间:2015-09-30 08:55:47

标签: javascript jquery

我想在文本框旁边显示错误消息,而不是在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;
    }
});`

1 个答案:

答案 0 :(得分:0)

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

相关问题