使用字符串或对象填充选择列表

发布于 2025-01-17 02:11:57 字数 1580 浏览 0 评论 0原文

这是我的第一篇文章,我是一个 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.

enter image description here

#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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

⒈起吃苦の倖褔 2025-01-24 02:11:57
jQuery('#bfLabel410').append(jQuery('<option value="">' + 'Choisir une option' + '</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] + '">' + result[i][0] + '</option>'));
} 
jQuery('#bfLabel410').append(jQuery('<option value="">' + 'Choisir une option' + '</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] + '">' + result[i][0] + '</option>'));
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文