0

我正在使用数据属性进行选择,我想根据第一个选择的数据属性进行过滤。这是我的代码,请告诉我什么?请原谅我的代码混乱,我是 javascript/Jquery 菜鸟

html块/////////

<select id="selectOne">
                    <option data-team='{"team":"Engineering"}'>Engineering </option>
                    <option   data-team='{"team": "Software"}'>Software Development</option>
                    <option data-team='{"team": "HumanResources"}'>Human Resources</option>
                    <option  data-team='{"team": "MarkettingAndSales"}'>Marketting and Sales</option>
                    <option  data-team='{"team": "Administrative"}'>Administrative</option>
                    <option  data-team='{"team": "Others"}' class="others">Others</option>
                </select>

                <select id="selectTwo">
                    <option  class="two" data-Speciality='{"team": "Software", "Speciality": "WebDevelopment"}'>Web Development </option>
                    <option  class="two" data-Speciality='{"team": "Engineering", "Speciality": "WebApplication"}'>Application Development </option>
                    <option  class="two" data-Speciality='{"team": "Software", "Speciality": "WebDevelopment"}'>UI/UX Design</option>
                    <option  class="two" data-Speciality='{"team": "Others", "Speciality": "ScientificResearch"}'>Scientific / Research</option>
                    <option  class="two" data-Speciality='{"team": "Engineering", "Speciality": "ScientificResearch"}'>Computer Hardware / Networking</option>
                    <option class="two" data-Speciality='{"team": "Others", "Speciality": "Others"}'>Others</option>
                </select>

$(function(){
    // function to use ajax calls to filter the table data depending on the select forms value
            $("#selectOne").change(function() {
                $("#selectTwo").hide();
                var selected = $(this).find('option:selected');
                $('#selectTwo option [data-team===selected]').show();



            console.log( $(selected).data('team') );

            //the filtering mechanism based on the class
         });

/// 显然我没有得到第二个选择的值任何线索为什么?

$(function(){
    // function to use ajax calls to filter the table data depending on the select forms value
        $("#selectOne").change(function() {
    var selected=$(this).find('option:selected').data('team').teamName;
    console.log(selected);
    var selectTwo= $('#selectTwo').val().data('Speciality').team;
     console.log(selectTwo);

})
4

2 回答 2

1

你不能这样显示/隐藏options,至少不是在每个浏览器中。

您必须积极地操作 DOM,即根据您的过滤器逻辑删除和添加元素,而您最终会用简单的 jQuery 搞得一团糟。

如果您需要这样的功能,我会选择任何将数据与 UI 客户端分离的方法:在这里您可以找到很多解决方案。

于 2013-01-08T11:42:11.690 回答
1

我想,你想要的是:

var selected = $(this).val();    
$('#selectTwo').val(selected).show();

不确定您的期望。

于 2013-01-08T11:43:50.897 回答