【发布时间】:2017-01-31 02:44:00
【问题描述】:
我需要一些 jquery 方面的帮助,我确信这很愚蠢,但让我陷入了困境。在第一个下拉列表中是产品列表。第二个是与产品相关的内容列表。我需要根据第一个下拉列表中选择的内容显示/隐藏第二个下拉列表中的正确内容。
例如:如果我在第一个列表中选择“FOO”,值为 1213,我需要从第二个列表中选择值 1213-3973 和 1213-3953。两个菜单中的值显然是动态的,但始终具有给定的格式,即:contentselect 选项中的值将始终为 {product_id}-{content_id}
起初,我对 html 不是超级了解,我在 contentselect 表单中设置了一个自定义属性,我让我的 jquery 选择器抓取了该属性。但是,似乎并非所有浏览器都喜欢自定义属性。
就像现在一样,我不确定该怎么做。有人告诉我我需要某种正则表达式,但我不知道从哪里开始。
<script>
function selectProduct()
{
//reset form
$('.form_contentselect option:selected').removeAttr("selected");
var product_selected = $('.form_productselect option:selected').val();
$('.form_contentselect').hide();
/////////HELP HERE PLEASE//////////
//$('.form_contentselect option[???????????]').hide(); //HIDE irrelevant content
//$('.form_contentselect option[???????????]').show(); //SHOW associated content
///////////////////////////////////
$('.form_contentselect').show();
}
</script>
<div class="form_productselect">
<div class="form_item">
Select Product:
<select name="product_id" onchange=selectProduct()>
<option value="0" selected="selected">--</option>
<option value="1213" >FOO</option>
<option value="1315" >BAR</option>
</select>
</div>
</div>
<div class="form_contentselect">
<div class="form_item">
Select Content:
<select name="content_id">
<option value="0" selected="selected">--</option>
<option value="1213-3973" >FOO - 100 points</option>
<option value="1213-3953" >FOO - 1000 points</option>
<option value="1315-3965" >BAR - 100 points</option>
<option value="1315-3949" >BAR - 1000 points</option>
</select>
</div>
</div>
【问题讨论】:
-
第二个元素应该是
multiple="multiple"吗? -
啊抱歉。现在我明白你想要什么了。只留下那些相关的值。
-
api.jquery.com/attribute-starts-with-selector,但是使用 ajax 动态加载内容会更好