2010-09-11 63 views
2
$('#request_song').autocomplete({ 
    serviceUrl: '<%= ajax_path("trackName") %>', 
    minChars:1, 
    width: 300, 
    delimiter: /(,|;)\s*/, 
    deferRequestBy: 0, //miliseconds 
    params: { artists: 'Yes' }, 
onSelect: function(value, data){ 
     artist = $('#request_artist').val(); //this will return "The Killers" 
    //make an ajax request to "/events/artist" 


}, 

我試圖讓ONSELECT函數內部的Ajax請求有沒有更好的辦法,如果沒有什麼是語法調用「/事件/殺手」那我需要的字符串替換呢?有沒有這樣做的乾淨方式有沒有更好的方式來做到這一點ajax調用

回答

2

如何通過jQuery調用ajax?像下面...

onSelect: function(value, data){ 
     artist = $('#request_artist').val(); //this will return "The Killers" 
    $.ajax({ 
    type: "POST", 
    url: "/events/" + artist, 
    data: "{}", 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    success: function(msg) { 
     alert(msg); 
    } 
}); 
} 

我建議要麼格式化「藝術家」變量服務器端,從而自動完成正確的格式返回藝術家,alertanively用正則表達式像這樣格式化...

url: "/events/" + formatName(artist), 

...

function formatName(artist) { 
    return artist.replace(/[\W]/, "") 
} 
相關問題