jQuery链接:如何取消链接下拉列表的约束?
我使用了这个插件: 我已经链接了下拉列表,在某些情况下,我想根据事件取消锁定并重新绑定该链接 这里有一些例子:jQuery链接:如何取消链接下拉列表的约束?,jquery,chained,Jquery,Chained,我使用了这个插件: 我已经链接了下拉列表,在某些情况下,我想根据事件取消锁定并重新绑定该链接 这里有一些例子: <select id="first"> <option value="a">A</option> <option value="b">B</option> </select> <select id="second"> <option value="c" class="a">C</op
<select id="first">
<option value="a">A</option>
<option value="b">B</option>
</select>
<select id="second">
<option value="c" class="a">C</option>
<option value="d" class="a">D</option>
<option value="e" class="b">E</option>
</select>
<input type="checkbox" value="1" id="unchain" />
已尝试$second.解除绑定'chained';但是没有起作用
有什么建议吗?链接插件会从第二次选择中过滤所有不匹配的选项,因此当您取消对更改事件的取消绑定时,第二次选择将有一些选项被切断,即永远丢失 只有在取消勾选后,您将使用全套选项重新初始化second select时,该选项才起作用。所以应该这样做:
$(function () {
// remember #second select
var secondCopy = $('#second').clone();
$('#second').chained('#first');
$('#unchain').change(function(){
if ($(this).prop('checked')){
$('#second').chained('#first');
}
else{
$('#first').unbind('change');
// remember selected value:
var value = $('#second').val();
// populate #second select with remembered options
$('#second').html(secondCopy.html());
// set saved value
$('#second').val(value);
}
});
});
尝试调用$'first'。取消绑定'change';。虽然这将从下拉列表中解除所有更改事件的绑定。啊,是的。它尝试使用$'second'。取消绑定'change';如果你想被锁回,只需$'second'。绑定'change';首先,您应该从第一次选择而不是第二次选择中取消绑定更改事件。但这仍然不起作用,因为链接插件会从第二次选择中过滤所有不匹配的选项,所以当您从更改事件中取消锁定解除绑定时,第二次选择将有一些选项被切断,即永远丢失。只有在取消勾选后,您将使用全套选项重新初始化second select,它才能工作。这对你有用吗?啊,你是对的。取消绑定秒将删除秒中的选项。你的解决方案对我有效。谢谢
$(function () {
// remember #second select
var secondCopy = $('#second').clone();
$('#second').chained('#first');
$('#unchain').change(function(){
if ($(this).prop('checked')){
$('#second').chained('#first');
}
else{
$('#first').unbind('change');
// remember selected value:
var value = $('#second').val();
// populate #second select with remembered options
$('#second').html(secondCopy.html());
// set saved value
$('#second').val(value);
}
});
});