我正在使用data attr进行选择,我想根据第一个select的data属性进行过滤。这是我的代码,请让我知道什么?请原谅我代码的混乱,我是一个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
});/显然我没有得到第二个select的值为什么?
$(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);
})发布于 2013-01-08 19:42:12
您不能那样显示/隐藏option,至少不能在每个浏览器中显示/隐藏。
你必须主动地操作DOM,比如根据你的过滤器逻辑移除和添加元素,这样你就只能用简单的jQuery把事情搞得一团糟。
如果您需要这样的功能,我会使用任何可以将数据从UI客户端解耦的功能:here you can find许多解决方案。
发布于 2013-01-08 19:43:51
我认为,你想要的是:
var selected = $(this).val();
$('#selectTwo').val(selected).show();不确定您所期望的是什么。
https://stackoverflow.com/questions/14214122
复制相似问题