JQuery' s:#not;'选择器不会阻止意图被排除的类(装饰元素)触发.keydown事件。为什么呢?
从下面的代码中,当我按下.newOwnerEntryInput字段中的某个键时,我希望看到' 1'只要。但我看到了两个警报' 1'和' 2'。
使用Javascript:
$('.newOwnerEntryInput').keydown(function (event) {
alert('1');
});
// Prevent Enter from submitting form.
$('form:not(.newOwnerEntryInput)').keydown(function (event) {
alert('2');
});
HTML:
<li style="position: relative">
@Html.DropDownList("cftMemberID", null, String.Empty, new { @class = "actionOwnerDropDown hidden" })
<div class="newOwnerEntryDiv">
<input class="newOwnerEntryInput" />
<div class="float-right closeNewOwner">
<img src="~/Images/cancel_x.png" alt="close" />
</div>
</div>
</li>
我尝试了各种引号样式,包含和不包含带引号的排除类,以及添加&#39;输入&#39;课后,如$(&#39;形式:not(.newOwnerEntryInput input)&#39;)。keydown
谢谢!
答案 0 :(得分:2)
感谢那些帮助过的人。我确实需要为所有类型的输入字段触发表单,而不仅仅是输入类型的表单。所以那就出局了。
这就解决了我的问题:
$('form').keydown(function (event) {
if (! event.which.hasClass('.newOwnerEntryInput')) {
alert('2');
}
});
在这种情况下,对于我的类.newOwnerEntryInput的输入,如果按下某个键,它将不会触发事件并将“2”推出到警报屏幕。
再次,谢谢,我需要一些回复,所有这些都有一个解决方案,让我自己回答这个问题。 :)
答案 1 :(得分:1)
试试这个:
HTML:
<div>
<input class="newOwnerEntryInput" type="text"/><br />
<!-- I know you have MVC dropdown list, but I replaced it with a html textbox (for simple testing) -->
<input class="newOwnerEntryInput1" type="text"/>
</div>
的JavaScript:
$('input.newOwnerEntryInput').keydown(function (e) {
alert('1');
});
$('input:not(.newOwnerEntryInput)').keydown(function (e) {
alert('2');
});
我查看了文档,在他们的例子中,我看到他们有元素input
后跟带选择器的函数。
可在此处获取文档:jQuery :not()
我希望这有帮助!
干杯!
答案 2 :(得分:-1)
试试这个:
$('form input:not(.newOwnerEntryInput)').on('keydown',function (event)
{
alert('2');
});