2015-11-10 73 views
4

新來的jQuery和Javascript在將jQuery自動完成JSON數據源,我不知道如何在動態驅動形式

$(function() { 
    var max_fields  = 10; //maximum input boxes allowed 
    var wrapper   = $(".input_fields_wrap"); //Fields wrapper 
    var add_button  = $(".add_field_button"); //Add button ID 
// autocomplete enablement 
    var availableAttributes = [ 
     "account_address", 
     "account_address_city", 
     "account_address_country", 
     "account_address_state", 
     "account_address_street1", 
     "account_address_street2", 
     "account_address_zip", 
     "account_email", 
     "account_login", 
     "account_name", 
     "account_number", 
     "account_telephone" 
    ]; 
    $("input[name^='mytext']").autocomplete({ 
     source: availableAttributes 
    });  

    var x = 1; //initlal text box count 
    $(add_button).click(function(e){ //on add input button click 
     e.preventDefault(); 
     if(x < max_fields){ //max input box allowed 
      x++; //text box increment 
      $(wrapper).append($('<div><input type="text" name="mytext[]"/><a href="#" class="remove_field">Remove</a></div>').find(":text").autocomplete({ 
     source: availableAttributes 
    })); //add input box 
     } 
    }); 

    $(wrapper).on("click",".remove_field", function(e){ //user click on remove text 
     e.preventDefault(); $(this).parent('div').remove(); x--; 
    }) 


    }); 

與(jQuery的傑森遠程數據源替換該自動完成

// autocomplete enablement 
    var availableAttributes = [ 
     "account_address", 
     "account_address_city", 
     "account_address_country", 
     "account_address_state", 
     "account_address_street1", 
     "account_address_street2", 
     "account_address_zip", 
     "account_email", 
     "account_login", 
     "account_name", 
     "account_number", 
     "account_telephone" 
    ]; 

自動完成。鏈接http://jqueryui.com/autocomplete/#remote-jsonp

$(function() { 
    function log(message) { 
     $("<div>").text(message).prependTo("#log"); 
     $("#log").scrollTop(0); 
    } 

    $("#city").autocomplete({ 
     source: function(request, response) { 
     $.ajax({ 
      url: "http://gd.geobytes.com/AutoCompleteCity", 
      dataType: "jsonp", 
      data: { 
      q: request.term 
      }, 
      success: function(data) { 
      response(data); 
      } 
     }); 
     }, 
     minLength: 3, 
     select: function(event, ui) { 
     log(ui.item ? 
      "Selected: " + ui.item.label : 
      "Nothing selected, input was " + this.value); 
     }, 
     open: function() { 
     $(this).removeClass("ui-corner-all").addClass("ui-corner-top"); 
     }, 
     close: function() { 
     $(this).removeClass("ui-corner-top").addClass("ui-corner-all"); 
     } 
    }); 
    }); 

完整的HTML,(jsfiddle

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 

<script type="text/javascript" src="jquery-2.0.2.min.js"></script> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.js"></script> 

<script> 
var availableAttributes = [ "account_address", "account_address_city", "account_address_country", "account_address_state", "account_address_street1", "account_address_street2", "account_address_zip", "account_email", "account_login", "account_name", "account_number", "account_telephone"]; 

$(document).ready(function() { 
    var max_fields = 10; //maximum input boxes allowed 
    var wrapper = $(".input_fields_wrap"); //Fields wrapper 
    var add_button = $(".add_field_button"); //Add button ID 

    var x = 1; //initlal text box count 
    $(add_button).click(function(e) { //on add input button click 
     e.preventDefault(); 
     if (x < max_fields) { //max input box allowed 
      x++; //text box increment 
      // $(wrapper).append('<div><input id="' + x + '" type="text" name="mytext"/><a href="#" class="remove_field">Remove</a></div>'); //add input box 
      $(wrapper).append('<div>' + '<input id="' + x + '" type="text" name="mytext"><br>' + '<textarea name="desc[]"></textarea>' + '<a href="#" class="remove_field">Remove</a>' +'</div>'); 
      $("input[id=" + x + "]").autocomplete({ 
       source: availableAttributes 
      }); 
     } 
    }); 

    $(wrapper).on("click", ".remove_field", function(e) { //user click on remove text 
     e.preventDefault(); 
     $(this).parent('div').remove(); 
     x--; 
    }) 
}); 

// autocomplete enablement 
$(function() { 
    $("input[name^='mytext']").autocomplete({ 
     source: availableAttributes 
    }); 
}); 

</script> 
</head> 

<body> 
    <div class="input_fields_wrap"> 
     <button class="add_field_button">Add More Fields</button> 
     <div><input type="text" name="mytext[]"></div> 
     <textarea name="desc[]"></textarea> 
    </div> 
</body> 
</html> 
+0

你使用打算什麼web服務工作的例子嗎? – BenG

+0

@ BG101,html css javascript,jQuery,php,mysql –

+0

網址我的意思是抱歉。 – BenG

回答

0

這裏是你貼什麼

$(document).ready(function() {  
 
    
 
$(".myText").autocomplete({ 
 

 
source: function (request, response) { 
 
jQuery.getJSON(
 
\t "http://gd.geobytes.com/AutoCompleteCity?callback=?&q="+request.term, 
 
\t function (data) { 
 
\t response(data); 
 
\t } 
 
); 
 
}, 
 
minLength: 3 
 
}); 
 

 
var max_fields = 10; //maximum input boxes allowed 
 
var wrapper = $(".input_fields_wrap"); //Fields wrapper 
 
var add_button = $(".add_field_button"); //Add button ID 
 

 
var x = 1; //initlal text box count 
 
$(add_button).click(function(e) { //on add input button click 
 
    e.preventDefault(); 
 
    if (x < max_fields) { //max input box allowed 
 
     x++; //text box increment 
 
     // $(wrapper).append('<div><input id="' + x + '" type="text" name="mytext"/><a href="#" class="remove_field">Remove</a></div>'); //add input box 
 
     $(wrapper).append('<div>' + '<input id="' + x + '" type="text" class="myText" id="myText"><br>' + '<textarea name="desc[]"></textarea>' + '<a href="#" class="remove_field">Remove</a>' +'</div>'); 
 
     
 
     $("#" + x).autocomplete({ 
 

 
source: function (request, response) { 
 
jQuery.getJSON(
 
\t "http://gd.geobytes.com/AutoCompleteCity?callback=?&q="+request.term, 
 
\t function (data) { 
 
\t response(data); 
 
\t } 
 
); 
 
}, 
 
minLength: 3 
 
}); 
 
    } 
 
}); 
 

 
$(wrapper).on("click", ".remove_field", function(e) { //user click on remove text 
 
    e.preventDefault(); 
 
    $(this).parent('div').remove(); 
 
    x--; 
 
}); 
 
});
<link href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" rel="stylesheet"/> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
 
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script> 
 
<div class="input_fields_wrap"> 
 
     <button class="add_field_button">Add More Fields</button> 
 
     <div><input type="text" class="myText" name="myText" id="myText"></div> 
 
     <textarea name="desc[]"></textarea> 
 
    </div>

+0

需要用http://jqueryui.com/autocomplete/#remote-jsonp –

+0

來替換上面的auto自動完成我已經更新了它,如果你想使用remote來加載它,你需要編寫自己的頁面來返回json。選項。但是,這個與你非常接近的jQuery例子 – Diginari

+0

一起工作。是的,但只適用於第一個領域。一旦您點擊「添加更多字段」按鈕,自動完成功能將停止在第二個字段上工作。在我接受你的答案之前,你能否糾正它? –