取消绑定更改事件



我在下拉列表更改时触发Change事件,我必须在更改时删除selected属性,下面是代码。。。

cshtml

<select id="ddlKeywords" name="KeywordsCollection" onchange="Images.ClearFieldsOnKeywordSelect()" multiple>
<option value="">Please select keywords</option>
@foreach (var item in ViewBag.keywords)
{
<option value="@item.Code">@item.Description</option>
}
</select>

Jquery

me.ClearFieldsOnKeywordSelect = function () {        
$('#ddlKeywords option[value=]').removeAttr('selected').change();
.........................

正如您所看到的,.change()事件在删除attr时被触发。

我试过unbind('change').change()off('change').change()unbind().change()off().change(),但都不起作用。

并且unbind('change')不移除selected属性

我认为您想要做的只是$('#ddlKeywords option[value=]').attr('selected', false),它将在不触发更改事件的情况下删除属性。

此外,您应该为要从中删除属性或根本不添加说明符的选项提供一个值。

为了进一步参考,unbind方法需要一个函数来解除绑定(它在后台使用removeEventListener函数。

最新更新