我有一個multiselect chzn-select-deselect框。當我選擇一個特定的值時,我想一次選擇多個值。我有以下的HTML:如何自動選擇多選框chzn-select-deselect?
<select id="filter_list_dropdwn" class="inp direct_value_opp fl" multiple="multiple" name="data[value1][]">
<option value="1" parent_id="0"> parent1</option>
<option value="2" parent_id="1"> child1 of parent1</option>
<option value="3" parent_id="1"> child2 of parent1</option>
<option value="4" parent_id="3"> child of child3</option>
</select>
如果我選擇parent1然後自動它的孩子將被選中。工作腳本是這樣的:
這裏是上述功能的小提琴:http://jsfiddle.net/NEXv3/
現在,我想申請相同的chzn-select-deselect
選項。所以,我修改了這樣的腳本:
$('#filter_list_dropdwn option:not(:selected)').live('click', function() {
unselected = $(this);
var parent_id = $(unselected).attr("value");
$('#filter_list_dropdwn option[parent_id=' + parent_id + ']').each(function (i, selected) {
$(this).attr('selected', false).chosen();
});
});
$('#filter_list_dropdwn option:selected').live('click', function() {
selected = $(this);
var parent_id = $(selected).attr("value");
$('#filter_list_dropdwn option[parent_id=' + parent_id + ']').each(function (i, selected) {
$(this).attr('selected', true).chosen();
});
});
但它沒有按預期工作。任何人都可以建議我在chzn-select-deselect
下拉菜單中應用相同的自動多選選項時出了什麼問題?
'parent_id'不是一個正確的屬性。使用'data-parentid'即使用'$(element).data('parentid')'。另外,考慮不再使用「live」,而是使用事件代表「on」。 – Brewal