2010-07-29 66 views
-1

我新受这是我的第一个项目,我不知道如何解决有关数据 我有一个输入页面的重复任何问题。 它由2个文本框的:如何显示警报“的数据已存在”

  1. 模型,

  2. 串行,

和一个组合框: 1.线。

我想,如果有两次输入, 我这里的意思是双输入数据, 将出来警示“数据已存在”。 ?

我怎么做,我尝试这样可是不行的:

$("#input").click(function() { 
     if($("#submit").valid()) { 
       var params=$("#submit").serialize(); 
       $.ajax({ 
         type:"post", 
         url:"process1.php", 
         data:params, 
         cache :false, 
         async :false, 
         success : function() { 
            $('input[name^="text"]').change(function() { 
              var $current = $(this); 
              $('input[name^="text"]').each(function() { 
               if ($(this).val() == $current.val() && $(this).attr('id') != $current.attr('id')) 
               { 
                alert('data already exists!'); 
                } 
              }); 
            }); 
            $("#showmodel").val($("#model").val()); 
            $("#showline").val($("#line").val()); 

回答

0
$(function() { 
     $("#input").click(function(e) { 
      var itemExists = false; 
      var txt = $("#Text1").val(); 
      e.preventDefault(); 
      $("#Select1 option").each(function() { 
       if ($(this).text() == $.trim(txt)) { 
        itemExists = true; 
        alert('Item already exists'); 
       } 
      }); 

      if (!itemExists) { 
      $("#Select1").append("<option value=\"1\">" + txt + "</option>"); 
      $("#Text1").val(''); 
      } 
     }); 
    });   
0

看看这个答案:

prevent Duplicate values using Jquery Validation

$(function(){ 

$('input[name^="text"]').change(function() { 

    var $current = $(this); 

    $('input[name^="text"]').each(function() { 
     if ($(this).val() == $current.val() && $(this).attr('id') != $current.attr('id')) 
     { 
      alert('data already exists!'); 
     } 

    }); 
    }); 
}); 
+0

对不起,我有变化我的问题 – klox 2010-07-29 08:45:47

+0

答案是相关的也是你的更新后,你看到的链接? – 2010-07-29 08:49:17

+0

对不起,我是新手..我知道如果一切都是文本框,但在我的情况下,我也有组合框。 – klox 2010-07-29 10:05:26