$('#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请求是否有更好的方法,如果不是什么将是调用“/ events / the killers”的语法,以及我需要的字符串替换怎么样。有没有干净的方法来做这个
答案 0 :(得分: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);
}
});
}
我建议格式化“艺术家”变量服务器端,以便自动完成以正确的格式返回艺术家,使用这样的正则表达式进行警告格式化...
url: "/events/" + formatName(artist),
...
function formatName(artist) {
return artist.replace(/[\W]/, "")
}