有人可以建议为什么这不加载自动填充数据?如果我在ajax调用中硬编码URL,它可以工作,但不能用我的代码来改变url源吗? 我不确定为什么它不起作用。如果您想要整体查看页面,则硬编码版本为:here
var starterSearchData;
$(function() {
var destination;
elementVal = $("input[name=radio]"); //note returns array of radio button elements
if (elementVal[0].checked){
destination= "http://learn.cf.ac.uk/webstudent/sem5tl/javascript/assignments/spanish.php",
}
if (elementVal[0].checked){
destination= "http://learn.cf.ac.uk/webstudent/sem5tl/javascript/assignments/italian.php",
}
//Starter Autocomplete (Spanish)
var starterSearchData;
$(function() {
$.ajax({
url: destination,
dataType: "jsonp",
async: false,
success: function(data) {
starterSearchData = $.map(data, function(item) {
if (item.course == "starter")
return item.name;
return item.price;
});
EnableAutoComplete();
},
error: function(xhr, status, error) {
var err = eval("(" + xhr.responseText + ")");
alert(err.Message);
}
});
function EnableAutoComplete() {
$("#starter").autocomplete({
source: starterSearchData,
minLength: 2,
delay: 010
});
}
});
单选按钮:
<div id="radio">
<input type="radio" id="radio1" name="radio"><label for="radio1">Spanish</label>
<input type="radio" id="radio3" name="radio"><label for="radio3">Italian</label>
</div>
答案 0 :(得分:0)
您的代码中的某些位置存在语法错误。这将有效:
var starterSearchData;
$(function() {
var destination,
elementVal = $("input[name=radio]");
if (elementVal[0].checked) {
destination = "http://learn.cf.ac.uk/webstudent/sem5tl/javascript/assignments/spanish.php";
}
if (elementVal[1].checked){
destination= "http://learn.cf.ac.uk/webstudent/sem5tl/javascript/assignments/italian.php";
}
$.ajax({
url: destination,
dataType: "jsonp",
async: false,
success: function(data) {
starterSearchData = $.map(data, function(item) {
if (item.course == "starter")
return item.name;
return item.price;
});
EnableAutoComplete();
},
error: function(xhr, status, error) {
var err = eval("(" + xhr.responseText + ")");
alert(err.Message);
}
});
function EnableAutoComplete() {
$("#starter").autocomplete({
source: starterSearchData,
minLength: 2,
delay: 010
});
}
});