每次在任何函数内部调用Typeahead脚本函数 提前输入未定义的错误 html代码:
<select id ='options' name='options' class='form-fields' onchange="myFunction();">
<option value='0' >Pick Type</option>
<option value="1" selected >train</option>
<option value="2">bus</option>
</select>
此javascript代码:
<script>
//typeahead changing according to selecting script
function myFunction() {
var option = document.getElementById("options").value;
if (option =='1'){
console.log(option);
$('input.typeahead').typeahead({
source: function (query, process) {
return $.get('typeahead-train.php', { query: query }, function (data) {
console.log(data);
data = $.parseJSON(data);
return process(data);
});
}
});
}else if(option =='2'){
console.log(option);
$('input.typeahead').typeahead({
source: function (query, process) {
return $.get('typeahead-publiccairo.php', { query: query }, function (data) {
console.log(data);
data = $.parseJSON(data);
return process(data);
});
}
});
}
}
我包含了'typeahead.js'aleady,
答案 0 :(得分:0)
我通过在typeahead函数内部调用onchange函数来解决此问题,如下所示:-
<script>
function myFunction() { //return diffrent values with different select options
var option = document.getElementById("options").value;
if (option==1){
typeahead ='typeahead-train.php';
}
else if (option ==2){
typeahead ='typeahead-publiccairo.php';
}
window.typeahead=typeahead;
return typeahead;
}
$('input.typeahead').typeahead({
source: function (query, process) {
return $.get(myFunction()/*onchange function called here*/, { query: query }, function (data) {
console.log(data);
data = $.parseJSON(data);
return process(data);
});
}
});