英文:
Get value in onSelect which is selected in autocomplete
问题
我正在尝试从AJAX中获取另一个值,但它给我一个错误Uncaught TypeError: e.replace is not a function
。
以下是我的代码。
source: function(name, response) {
$.ajax({
type: "GET",
dataType: "json",
url: af_vars.af_ajax_url,
data: "action=get_listing_names&name=" + name,
success: function(data) {
var tickers = [];
$.map(data, function(item) {
for (var index = 0; index < item.length; index++) {
tickers.push(item[index]['tickers']);
}
});
console.log(tickers);
response(tickers);
},
error: function(response) {}
});
},
onSelect: function(e, term, item) {
//我的代码
}
这是我的PHP AJAX请求代码。
$titles = array();
if (!empty($results)) {
foreach ($results as $term) {
$data = array();
$data['tickers'] = get_term($term->parent, 'exchange')->name ? esc_attr(get_term($term->parent, 'exchange')->name) . ' : ' . addslashes($term->name) : addslashes($term->name);
$data['isArchived'] = get_term_meta($term->term_id, 'ticker_archived', true) == 1 ? 'true' : 'false';
$titles[] = $data;
}
} else {
$titles[] = "No results found";
}
echo json_encode(array(
'results' => $titles
));
因此,我想在下拉菜单中显示股票代码(tickers),并希望将isArchive结果作为隐藏值传递,以便在onSelect
中一起传递。
英文:
I am trying to catch another value from AJAX but it is giving me an error Uncaught TypeError: e.replace is not a function
.
Here is my code.
source: function(name, response) {
$.ajax({
type: "GET",
dataType: "json",
url: af_vars.af_ajax_url,
data: "action=get_listing_names&name=" + name,
success: function(data) {
// response(data);
var tickers = [];
$.map( data, function( item ) {
for (var index = 0; index < item.length; index++) {
tickers.push(item[index]['tickers']);
}
});
console.log(tickers);
response(tickers);
},
error: function(response) {}
});
},
onSelect: function(e, term, item) {
//my code
}
and here is my PHP ajax request code
$titles = array();
if (!empty($results)) {
foreach ($results as $term) {
$data = array();
$data['tickers'] = get_term( $term->parent, 'exchange' )->name ? esc_attr( get_term( $term->parent, 'exchange' )->name ) .' : '. addslashes($term->name) : addslashes($term->name) ;
$data['isArchived']= get_term_meta( $term->term_id , 'ticker_archived', true) == 1 ? 'true' : 'false';
$titles[] = $data;
}
}else {
$titles[] = "No results found";
}
echo json_encode(array(
'results' => $titles
)
);
So, I want to show tickers in dropdown and want isArchive result as hidden value to pass along with tickers in onSelect
答案1
得分: 2
你可以尝试这种方式,对我来说有效。或者如果你能将它放在服务器上并给我访问权限,那么我也可以在那里检查。
source: function(query, process) {
var objects = [];
map = {};
$.ajax({
url: baseURL + "/admin/getmember",
data: 'q=' + query,
dataType: "json",
type: "GET",
success: function (data) {
console.log(data);
$.each(data, function(i, object) {
map[object.membername] = object;
objects.push(object.membername);
});
process(objects);
}
});
},
updater: function(item) {
$('#hiddenId').val(map[item].id);
return item;
}
PHP代码:
$memberResults = array();
foreach ($members as $k => $member) {
$memberResults[$k]['id'] = $member->id;
$memberResults[$k]['membername'] = $member->fname . ' ' . $member->lname;
}
英文:
You can try this way it is working for me. or if you can put it on server and give me access so i can check that there as well as.
source: function(query, process) {
var objects = [];
map = {};
$.ajax({
url: baseURL+"/admin/getmember",
data: 'q=' + query,
dataType: "json",
type: "GET",
success: function (data) {
console.log(data);
$.each(data, function(i, object) {
map[object.membername] = object;
objects.push(object.membername);
});
process(objects);
}
});
},
updater: function(item) {
$('#hiddenId').val(map[item].id);
return item;
}
PHP code
$memberResults = array();
foreach($members as $k=>$member){
$memberResults[$k]['id'] = $member->id;
$memberResults[$k]['membername'] = $member->fname.' '.$member->lname;
}
答案2
得分: 2
你可以注册任何变化:
$('html').bind('input', function() {
alert('test');
});
另一个选项是在失去焦点时(离开元素焦点)进行更新:
$("input").on('blur keyup change click', function () {
英文:
You could register to any change:
$('html').bind('input', function() {
alert('test');
});
Other option is to get update on blur (leaving focus of element)
$("input").on('blur keyup change click', function () {
答案3
得分: 1
"Uncaught TypeError: e.replace is not a function" 意味着 "变量 'e' 不是一个字符串。"
除此之外.......由于变量 "e" 用在写作为 " //我的代码 " 的部分,你没有分享代码中最重要的部分(包含错误的部分在这里不可见)。
你能展示一下 "OnSelect" 函数内部的内容吗?或者给我们一个 CodePen?
英文:
"Uncaught TypeError: e.replace is not a function" means "The variable 'e' is not a string."
Beyond that.......Being that the variable "e" is used in the part that's written as " //my code ", you aren't sharing the most important part of your code (The part that contains an error isn't visible here).
Can you show us what's inside the "OnSelect" function? Perhaps give us a codepen?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论