2012-04-13 30 views
0

我已經制作了一段jQuery代碼,用於動態添加新的文本字段。 它也會檢查字段的數量。無法爲此獲得一個好的jQuery ajax代碼

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html> 

<head> 
<style type="text/css"> 

    fieldset { 
     margin-bottom: 1em; 
    } 

    input { 
     display: block; 
     margin-bottom: .25em; 
    } 

    #print-output { 
     width: 100%; 
    } 

    .print-output-line { 
     white-space: pre; 
     padding: 5px; 
     font-family: monaco, monospace; 
     font-size: .7em; 
    } 

    .enter { 
     padding-bottom: 15px;72-96027-0008 

    } 

</style> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> 
</head> 

<body onload="doIt();"> 

<form> 
    <div id="fields"> 
     <input id="input_'+ i +'" type="text" name="barcodes[]" class="target" /><span id="status_'+ i +'"></span><br /> 
    </div> 
</form> 

<script type="text/javascript"> 
    function doIt() 
    { 
     var i = $('input').size() + 1; 

     $("input:last").focus(); 
     $("input:last").keypress (function (e) 
     { 
      if (e.which == 13) 
      { 
       e.preventDefault(); 

       $("#input_'+ i +'").change(function(){ 

       var barcode = $("input_'+ i +'").val 
       if(barcode.length > 0){ 
        $("#status_'+ i +'").html(''); 

        $.ajax({ 
         type: "POST", 
         url: "text.txt", 
         data: "barcode="+ username, 
         success: function(server_response){ 
          $("#status_'+ i +'").ajaxComplete(function(event, request){ 
           if(server_response == '0'){ 
            $("#status_'+ i +'").html('<font color="red">On bekdende Barcode. probeer het opnieuw.</font>'); 
           }else if(server_response == '1'){ 
            $("#status_'+ i +'").html(''); 

            $("#fields").append ('<div class="enter" id="input_'+ i +'"><input type="text" name="barcodes[]" class="target" /><a href="#" onclick="verwijder (\'' + i + '\')">Delete</a><span id="status"></span></div><div id="error"></div>'); 
            $("#nItems").html("Er zijn " + (i - 1) + " items gescant"); 
           } 
          }); 
         } 
        }); 
       } 
       doIt(); 
      }); 
     } 
    }); 

    doIt(); 

    function verwijder (i) 
    { 
     $("#input_" + i).remove(); 
    } 
</script> 

<span id="nItems">Er zijn nog geen items gescant</span> 

</body> 

</html> 

但現在我想要它來檢查字段中的輸入是否存在。 它不得不說,如果它不exsists。 我已經嘗試了很多,沒有任何不工作。

幫助將被折衷。

+0

你是什麼意思正好與「中輸入的字段」?你的意思是你需要檢查輸入是否有任何價值? – 2012-04-13 07:53:32

+0

當我在字段中添加1時,如果數據庫中有1,則必須檢查它。等等 – ABTOMAT 2012-04-13 07:55:41

+1

爲什麼你的輸入id _literally_「input_'+ i +'」?似乎你試圖把一個動態的編號在那裏,但這不是如何工作...... – 2012-04-13 07:56:17

回答

0

試試這個:

$.ajax({ 
    type: 'HEAD', 
    url: 'link to chek with db', 
    success: function() { 
     alert('Item found.'); 
    }, 
    error: function() { 
     alert('Item not found.'); 
    } 
});