2012-04-13 50 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.'); 
    } 
});