简体   繁体   English

Selectbox使用jQuery / AJAX更改内容

[英]Selectbox change content using jQuery/AJAX

I am trying to find best solution for this: I will have categories, subcategories, sub-subcategories etc. and I need to have it in different selectboxes. 我正在尝试为此找到最佳解决方案:我将具有类别,子类别,子子类别等,并且需要在不同的选择框中使用它。 Example: First there will be: 示例:首先将是:

<select>
    <option value="cars">Cars</option>
    <option value="electronic">Electronic</option>
    <option value="garden">Garden</option>
</select>

After that when user choose for example Cars this select box changes to 之后,当用户选择“汽车”时,此选择框将变为

<select>
    <option value="volvo">Volvo</option>
    <option value="saab">Saab</option>
    <option value="mercedes">Mercedes</option>
    <option value="audi">Audi</option>
</select> 

Concept of page is just one box in the middle of page and after you choose first category this box will disappear and there come new box with subcategory. 页面的概念只是页面中间的一个框,选择第一个类别后,该框将消失,并出现带有子类别的新框。 What is the best solution for that and how to do it in AJAX? 最好的解决方案是什么,以及如何在AJAX中实现呢?

I just wrote this JSFIDDLE: http://jsfiddle.net/thauwa/xGFQN/ 我刚刚写了这个JSFIDDLE: http : //jsfiddle.net/thauwa/xGFQN/

Basically, what you have to do is use some code like this: 基本上,您要做的是使用如下代码:

HTML HTML

<select id="optionsSet1" class="justAnotherDropdown">
    <option value=""></option>
    <option value="cars">Cars</option>
    <option value="electronic">Electronic</option>
    <option value="garden">Garden</option>
</select>
<select id="optionsSet2" class="justAnotherDropdown"></select>
<select id="optionsSet3" class="justAnotherDropdown"></select>
<select id="optionsSet4" class="justAnotherDropdown"></select>

jQuery jQuery的

$(".justAnotherDropdown").not($(".justAnotherDropdown").first()).hide(); // Hide everything but the first of the <select> tags
$(".justAnotherDropdown").change(function () { // The jQuery event handler.
    var changedID = $(this).attr('id');
    var prefixID = changedID.slice(0, -1);
    var nextIDNumber = parseInt(changedID.substr(changedID.length - 1)) + 1;
    var nextID = prefixID + nextIDNumber;
    var nextOptionsSet = "optionsSet" + nextIDNumber;
    $.ajax({
        url: 'yourPHPpage.php', // Change this to your PHP script's actual path.
        data: {
            value: $(this).val(), // Uncomment this line when using with PHP.
            id: changedID, // Uncomment this line when using with PHP.
        },
        type: "POST",
        success: function (response) {
            $("#" + changedID).hide(); // Hide the previous box.
            $("#" + nextID).html(response).show("slow"); // Drammatically show the next.
        }
    });
});

PHP PHP

    $selectedItem = $_POST['id'];
    $selectedItemValue = $_POST['value'];
    switch ($selectedItem) {
        case 'optionsSet2':
            switch ($selectedItemValue) {
                case 'Cars':
                    echo 'HTML to display the relevant `options`, properly escaped.';
                    break;
                case 'Electronic':
                    echo 'HTML to display the relevant `options`, properly escaped.';
                    break;
                case 'Garden':
                    echo 'HTML to display the relevant `options`, properly escaped.';
                    break;
            }
            break;
        case 'optionsSet3':
            /** Add more code here. **/
    }
/** This method is very inefficient, and 'strainful', compared to using JSON. **/ 

I hope that the code is self-explanatory. 我希望代码是不言自明的。

Dera Geril, what you are looking for is usually referred to as "cascading dropdown". Dera Geril,您正在寻找的通常称为“级联下拉列表”。 There are tons of pages about the argument and it really depends on which technology you are using. 关于论点的页面很多,它实际上取决于您使用的技术。 Google it for literally thousands of resources, both in terms of tutorials, open source and licensed code. Google为其提供了数千种资源,包括教程,开放源代码和许可代码。

To point out a couple of questions to ask yourself for better decision-making: 指出几个问题,以要求您做出更好的决策:

  1. Do I use any particular javascript framework? 我是否使用任何特定的javascript框架? Is there code developed specifically for it? 是否有专门为此开发的代码?
  2. Do I need data coming from Ajax (as per your questions, yes, but it's worth pointing it out)? 我是否需要来自Ajax的数据(根据您的问题,是的,但是值得指出)?
  3. Do I need the cascading dropdown to work even with Javascript disabled? 即使禁用了JavaScript,我是否也需要级联下拉菜单来工作?
  4. Do I want to write my own jQuery code to manage my ajax calls? 我是否要编写自己的jQuery代码来管理我的Ajax调用? Do I need more than updating the select boxes? 除了更新选择框,我还需要什么吗?

Possibly other considerations could be made. 可能还有其他考虑。

Hope you find it useful. 希望你觉得它有用。

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

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