简体   繁体   English

使用PHP中的动态选项进行选择

[英]Select with Dynamic Options in PHP

How to make a select list with dynamic options? 如何创建带有动态选项的选择列表?

I have two select list; 我有两个选择列表; I have two options on my first select list, each one have a different list of options in another select list. 我的第一个选择列表中有两个选项,另一个选择列表中的每个选项都有一个不同的选项列表。

If I choose the first select option, the second list should have a different values. 如果选择第一个选择选项,则第二个列表应具有不同的值。

First Select                           Second Select
-----------------                      -----------------                         
Option selected: [0] StackOverflow         Options Available:
Another option: [1] GitHub                   Ask a Question
                                             Answer a Question
-----------------     ---->            -----------------

or if the second value is selected 或如果选择第二个值

First Select                           Second Select
-----------------                      -----------------                         
Option selected: [1] GitHub                Options Available:
Another option: [0] StackOverflow            Post a New Repository
                                             Fork a Repository
-----------------     ---->            -----------------

I also use PHP with MySQL to display values (options) in the select. 我还使用PHP和MySQL在select中显示值(选项)。 In first select, I use to display all the possible options with MySQL, then echo it out. 在第一个选择中,我用来显示MySQL的所有可能选项,然后将其回显。

The second select list need the ID from the previously selected so it can select it. 第二个选择列表需要先前选择的ID,以便可以选择它。 Example SQL code: " SELECT * FROM sections WHERE id = ? ". 示例SQL代码:“ SELECT * FROM sections WHERE id = ? ”。 I'm new to PHP and to AJAX, I also see this question but it just doesn't work for me. 我是PHP和AJAX的新手,我也看到了这个问题,但这对我不起作用。 Sorry for re-asking this dumb question again. 很抱歉再次提出这个愚蠢的问题。

Initially, Hide the second selectbox. 最初,隐藏第二个选择框。 Add an onchange function to first selectbox in jquery. 将onchange函数添加到jquery中的第一个选择框。 Like below. 像下面。

If the second select box has static. 如果第二个选择框具有静态。

$('.firstselect').onchange(function() {
  var first_val = $(this).val();

  if ( first_val === 0 ) {
    $('stack_select').show();
    $('github_select').hide();

  } else if( first_val === 1 ) {
    $('stack_select').hide();
    $('github_select').show();
  }

});

If the second select box has dynamic. 如果第二个选择框具有动态。 Make an ajax request and load the content dynamically using jquery append method. 发出ajax请求,并使用jquery append方法动态加载内容。

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

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