简体   繁体   English

动态ajax / php选择标签

[英]Dynamic ajax/php select tag

Good evening, I got a small issue with my dynamic select thing like already said in the title. 晚上好,我在动态选择中遇到了一个小问题,如标题中所述。 My goal is to reach the following result: If someone selects an instrument from the first select tag, the other tag called "Besetzung" should filter a list of users who play the selected instrument, for the second select tag like shown in the screenshot (Instrument = instrument; E-Bass = electric bass; Besetzung = occupation; Offen = open) 我的目标是达到以下结果:如果有人从第一个选择标签中选择了一种乐器,则另一个名为“ Besetzung”的标签应过滤播放所选乐器的用户列表,第二个选择标签如屏幕截图所示(乐器=乐器;电子低音=电贝司; Besetzung =职业; Offen =打开)

This works fine so far. 到目前为止,这个工作正常。 But my problem is, that i got like 3 to 10 of these blue boxes (screenshot). 但是我的问题是,我得到了3到10个这样的蓝色框(截图)。 You can add these boxes manually over a button. 您可以在按钮上手动添加这些框。 And every single box should contain these "individual" select tags... So i need something like a unique ID for each select tag, each time a box is added. 而且每个单独的盒子都应包含这些“单独的”选择标签...因此,每次添加一个盒子时,我需要为每个选择标签提供唯一的ID之类的东西。 So this is my function for the dynamic selection so far: 到目前为止,这是我用于动态选择的功能:

function fetch_select(val)
{
    $.ajax({
     url: 'get_data.php',
     type: 'POST',
     data: {
        get_option:val.value
     },
     success: function (response) {
       document.getElementById("studentSelect").innerHTML=response; 
     }
  });
}

If the function succeeds the element with the ID "studentSelect" is changed. 如果函数执行成功,则ID为“ studentSelect”的元素将被更改。 But what I need is something like an array or so, which is used in my html, php and ajax to change every single element... I got no idea how to give every single select tag a unique ID because the number of tags will change while someone is using the website. 但是我需要的是一个类似数组的东西,在我的html,php和ajax中使用它来更改每个元素...我不知道如何给每个单独的select标签一个唯一的ID,因为标签的数量会在有人使用网站时进行更改。

I hope you understood what I mean, was a bit difficult to explain for me. 希望您理解我的意思,对我来说有点难以解释。 Thanks for any help in advance. 感谢您的任何帮助。

What you would normally do is use a common class for all elements involved. 通常,您将对所有涉及的元素使用一个通用类。

Then within the change events you can get the specific instance of the select class that was changed and use that to traverse to the corresponding other select to poplulate 然后,在更改事件中,您可以获取已更改的选择类的特定实例,并使用该实例遍历到相应的其他选择以进行填充

Repeating structure 重复结构

<div class="row">
   <select class="select-1"></select>
   <select class="select-2"></select>
</div>

JS JS

$(document).on('change', '.select-1', function(){
   // traverse to closest row, then look within that row for matching select
   var $otherSelect = $(this).closest('.row').find('.select-2');
    $otherSelect.load('get_data.php', {get_option: this.value})
            .fail(function(){ alert('Oops something went wrong')});
});

So you have two select lists, the second one depends of what is selected in the first one. 因此,您有两个选择列表,第二个选择列表取决于第一个选择的列表。 That is a very usual task in questionnaires. 这是问卷调查中非常常见的任务。

The first thing you did, is to change the second select list via ajax,trigger by a change in the first select list right? 您所做的第一件事是通过ajax更改第二个选择列表,通过更改第一个选择列表来触发吗? (that is fine) (那也行)

So you basically need is the php(server side code) that returns the html of the 2d select list. 因此,您基本上需要的是返回2d选择列表html的php(服务器端代码)。 Isn't it? 是不是

Whatever, what we need here is the list of options for each option of the first list. 无论如何,我们这里需要的是第一个列表中每个选项的选项列表。 That info should go server side in tables or arrays if you want. 如果需要,该信息应以表或数组的形式进入服务器端。

if you provide the options list for each option of the first list then I or somebody else can help you with the php for it. 如果您为第一个列表的每个选项提供选项列表,那么我或其他人可以为您提供PHP帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM