【问题标题】:How to show alert for duplicated value of textbox如何对文本框的重复值显示警报
【发布时间】:2016-03-07 20:30:17
【问题描述】:

我有多个具有不同值的 input 字段。我可以编辑它们但不能为空(如果为空则显示警报)。当我将其编辑为新值时,如果新值与任何其他输入值匹配,我需要一个警告您不能使用它

HTML

<input type="text" class="selector" value="new">
<input type="text" class="selector" value="old">
<input type="text" class="selector" value="newest">
<input type="text" class="selector" value="oldest">
<input type="text" class="selector" value="older">

JavaScript

$('.selector').change(function () {
    alert();
});

小提琴here

【问题讨论】:

    标签: javascript jquery html validation input


    【解决方案1】:

    试试这个代码:

    $('.selector').on('blur',function () {
       if ($('.selector[value="'+$(this).val()+'"]').not($(this)).length > 0 || current_value.length == 0 ) {
        alert('duplicate value');
       }
    });
    

    http://jsfiddle.net/0c0qep6y/12/

    【讨论】:

    • 几乎正确,但是光标会移到原来的那个,我希望它在重复的那个
    • 在我在警报上按确定并单击该输入后,它正在使用重复的值
    • on 函数更改为change。如果我使用on 函数,警报即将到来。当我将整个文本框从空编辑为 newest 时,我收到两个警报。我认为这是一种糟糕的用户体验。
    • 也取空值
    【解决方案2】:

    Updated Fiddle.

    您可以先使用$(this).attr('value', $(this).val()); 刷新值属性,然后使用值选择器$('.selector[value="' + current_value + '"]').length 检查是否有任何其他类selector 具有相同值的字段。

    希望这会有所帮助。


    $('body').on('blur', '.selector', function () {
        $(this).attr('value', $(this).val());
    
        if ($('.selector[value="' + $(this).val() + '"]').length > 1 ) {
            $(this).focus();
            alert('You cannot use this');
        }else if($(this).val().length == 0) {
            alert('Empty value not accepted');
        }
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <input type="text" class="selector" value="new">
    <input type="text" class="selector" value="old">
    <input type="text" class="selector" value="newest">
    <input type="text" class="selector" value="oldest">
    <input type="text" class="selector" value="older">

    【讨论】:

    • if ($('.selector[value="' + $(this).val() + '"]').length) { alert('Cannot use'); {
    • 感谢@Tushar 的建议,更新了我的答案,但认为我们必须使用length &gt; 1,因为我们有这个值的当前输入,请检查。
    • 如何在点击确定后将光标带回重复的文本框?
    • @IamRaviteja $('.selector[value="' + $(this).val() + '"]').not(this).first().focus() 或聚焦当前元素 $(this).focus()
    • 不工作here,当我将最旧的更改为旧的时
    【解决方案3】:

    $('.selector').change(function () {
      var changedItem = $(this);
      var otherSelectors = $(".selector").not(changedItem);
      var matchingItemPresent = [];
      $.each(otherSelectors, function (count, item) {
        $(item).val().trim()== changedItem.val().trim();
        matchingItemPresent.push($(item));
      });
      
      matchingItemPresent.length > 0 ? alert("Duplicate entry..same value exists in another field") : $.noop();
      
      matchingItemPresent[0].focus();
      console.log(matchingItemPresent[0]);
    });

    更新为专注于文本框JS Updated

    【讨论】:

      【解决方案4】:

      为每个输入文本字段使用唯一的 ID,并在您的 JavaScript 方法中使用 getElementById("[YourDefinedId]") 获取它们的值,并将它们相互比较并显示更改。

      HTML

      <input type="text" class="selector" id="first" value="new">
      <input type="text" class="selector" id="second" value="old">
      <input type="text" class="selector" id="third" value="newest">
      <input type="text" class="selector" id="fourth" value="oldest">
      <input type="text" class="selector" id="fifth" value="older">
      

      JavaScript

      function compareValues(){
      
      var first = getElementById("first").value;
      var second = getElementById("second").value;
      var third = getElementById("third").value; 
      var fourth = getElementById("fourth").value;
      var fifth = getElementById("fifth").value;
      
      //Now you can compare them using null check and empty.
      
      }
      

      【讨论】:

      • 我不能使用,它更复杂,因为我需要与每个id进行比较
      • 您可以动态创建id 并将其分配给inputText,这将帮助您使用循环进行比较。
      猜你喜欢
      • 2015-08-08
      • 2019-04-21
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 2019-08-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多