2010-07-29 63 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