简体   繁体   English

Js,根据先前的下拉选项选择输出选项下拉列表

[英]Js, output a option dropdown list depending on previous dropdown option choice

[SOLVED] [解决了]
See Dynamic dropdownlist value from database if you have a simular issue, it worked for me. 如果您遇到类似问题,请参阅数据库中的动态下拉列表值 ,它对我有用。

Trying to output a dropdown list, which is populated depending on a previous dropdown. 尝试输出一个下拉列表,该列表取决于先前的下拉列表。

The problem is, 问题是,

How do you make the second dropdown list dynamic? 如何使第二个下拉列表动态化?

The values i need to add, in the second dropdown box comes from a database. 我需要在第二个下拉框中添加的值来自数据库。
The results are stored in an php array ($rs). 结果存储在php数组($ rs)中。

Old php & html which used to do it, before i needed the options to change depending on previous selection. 在我需要根据之前的选择更改选项之前,曾经使用过的旧php&html。

<select name="re_id" id="re_id">
<?php foreach ($rs as $r) { ?>

<option value="<?php echo $r['r_id'] ?>">
<?php echo $r['name'] ?></option>

<?php } ?>
</select>



JS - Need to do what the old php is doing, aswell as sort by selection from first dropdown. JS-需要做旧php所做的事情,以及从第一个下拉列表中进行选择排序。

var Select_List_Data = {

're_id': {


    1: {
        text: ['option1', 'option2', 'option3', 'option4', 'option5'],
        value: ['option1', 'option2', 'option3', 'option4', 'option5']
    },

    2: {
        text: ['option6'],
        value: ['option6']
    }

}

};

And yes, ofc i have other js code that outputs it into the html form. 是的,ofc我还有其他js代码可以将其输出为html形式。

This code is however the issue. 但是,此代码是问题。

Basicly, 基本上,
if "1" is the value selected in the first dropdown, it will display option1-5 in the second dropdown. 如果“ 1”是在第一个下拉菜单中选择的值,它将在第二个下拉菜单中显示option1-5。

The js code needs to be more dynamic, so values 1, 2, x+(there are more than 1 & 2 in the DB) aswell as all the second dropdown options text & values are inside some kind of js foreach.., simular to the php foreach if this is even possible. js代码需要更具动态性,因此值1、2,x +(数据库中的值大于1和2)以及所有第二个下拉选项文本和值都在某种js foreach内。类似于如果可能的话,PHP foreach。

Any help, advice or link to useful information on the subject is appreciated, i have been unable to find anything useful myself.. 任何帮助,建议或有关该主题的有用信息的链接,我们感激不尽,我自己找不到任何有用的信息。

My suggestion: use AJAX 我的建议:使用AJAX

What you described is a classic problem in web-design. 您所描述的是网页设计中的一个经典问题。 One of the most popular recommendations in this case is to use an XHR , or - in other words - perform an AJAX request . 在这种情况下,最流行的建议之一是使用XHR ,或者换句话说,执行AJAX请求 This is especially useful if you intend to make a new database query when the select value changes, and then populate an additional select with up-to-date data. 如果打算在select值更改时进行新的数据库查询,然后使用最新数据填充其他select ,则此功能特别有用。

Compared to prepopulating all possible additional select fields, this approach will: 与预填充所有可能的其他select字段相比,此方法将:

  • significantly reduce bandwidth, especially if you have a large selection of selects , 显著减少带宽,有一个大的选择,特别是如果你selects
  • enables received data to be as up-to-date as possible. 使接收到的数据尽可能最新。

Just a quick outline of what I'm thinking about: 简要概述一下我的想法:

HTML 的HTML

<select id="first-select">
    <option value="1">one</option>
    <option value="2">two</option>
    <option value="3">three</option>
</select>

<select id="second-select">
    <option selected disabled>Please choose first</option>
</select>

JavaScript 的JavaScript

document.getElementById('first-select').addEventListener('change', function (e) {
    var xhr = new XMLHttpRequest();
    var elem = e.target || e.srcElement;

    // dropdowndata.php will echo whatever data it gets from a database query, possibly in JSON
    xhr.open('dropdowndata.php?value=' + elem.value, 'GET');
    xhr.onload = function () {
        // populate `select#second-select` using response from server (eg.: xhr.responseText)
        // ...
    }
    xhr.send();
});

You can find a concrete example at http://roshanbh.com.np/2007/12/change-dropdown-list-options-values-from-database-with-ajax-and-php.html 您可以在http://roshanbh.com.np/2007/12/change-dropdown-list-options-values-from-database-with-ajax-and-php.html中找到具体示例

If you are not familiar with this approach, I strongly recommend reading up on AJAX and JSON, they are very powerful tools, and likely just what you need here. 如果您不熟悉这种方法,我强烈建议您阅读AJAX和JSON,它们是非常强大的工具,可能正是您所需要的。

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

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