使用字符串或对象填充选择列表
这是我的第一篇文章,我是一个 javascript 新手,英语不是我的母语,所以很抱歉,如果我的解释不符合你的期望。
因此,我尝试使用 ajax 数据库查询的结果填充选择列表。 查询返回一个字符串:
'[["Saphir","1","10"],["Serval","2","10"],["Sygma","3","10"],["Swan","4","10"]]'
我的目的是使用此结果填充选择列表,例如第一个选项: Saphir 作为文本,“1”作为值。
这是我的最后一个代码,Chrome 控制台没有返回错误。 我尝试过适应到处发现的许多其他内容,但我仍然没有成功。 此代码填充一个列表,但不允许任何选择。
#bfLabel410是选择列表 ID
function ff_sidefab_action(element, action) {
switch (action) {
case 'change':
jQuery.ajax(
{
type: "POST",
url: "/BMWK1census/ajaxsidemodelist.php",
data: { code: ff_getElementByName('sidefab').value },
success: function (data) {
var obj = JSON.parse(data);
console.log(obj);
var result = [];
var keys = Object.keys(obj);
console.log(typeof keys)
keys.forEach(function (key) { result.push(obj[key]); });
jQuery('#bfLabel410').empty();
jQuery('#bfLabel410').append(jQuery('<option>', { value: '', text: 'Choisir une option' }));
for (var i = 0; i < result.length; i++) {
console.log(result[i]);
alert(result[i][0]);
jQuery('#bfLabel410').append(jQuery('<option>', { value: result[i][1], text: result[i][0] }));
} // fin boucle for
} // fin success
} // fin params ajax
); // fin jQuery.ajax()
break;
default: ;
} // fin switch
}
this is my first post, i am a javascript newbie, and english is not my native language, so sorry if i my explanations are not as you expect.
So, i am trying to populate a select list with the result of an ajax database query.
the query returns a string:
'[["Saphir","1","10"],["Serval","2","10"],["Sygma","3","10"],["Swan","4","10"]]'
My purpose is to use this result to populate a select list with e.g. for the first option:
Saphir as the text, and "1" for the value.
Here is my last code, no error returned by the Chrome console.
I have tried to adapt so many others found here and there, but i still didn't succeed yet.
this code fills a list, but doesn't allow any selection.
#bfLabel410is the select list ID
function ff_sidefab_action(element, action) {
switch (action) {
case 'change':
jQuery.ajax(
{
type: "POST",
url: "/BMWK1census/ajaxsidemodelist.php",
data: { code: ff_getElementByName('sidefab').value },
success: function (data) {
var obj = JSON.parse(data);
console.log(obj);
var result = [];
var keys = Object.keys(obj);
console.log(typeof keys)
keys.forEach(function (key) { result.push(obj[key]); });
jQuery('#bfLabel410').empty();
jQuery('#bfLabel410').append(jQuery('<option>', { value: '', text: 'Choisir une option' }));
for (var i = 0; i < result.length; i++) {
console.log(result[i]);
alert(result[i][0]);
jQuery('#bfLabel410').append(jQuery('<option>', { value: result[i][1], text: result[i][0] }));
} // fin boucle for
} // fin success
} // fin params ajax
); // fin jQuery.ajax()
break;
default: ;
} // fin switch
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)