简体   繁体   中英

negate one textbox from $('input[type=“text”]')

I have 4 textboxes, I wrote some code:

$('input[type="text"]').keyup(function () {
    if ($(this).val() == '') {
...

I do not want to apply this code to 1 texbox, what should I do ?

Give that textbox an id and update your code like this:

$('input[type="text"]:not("#id_of_txt")').keyup(function () {
    if ($(this).val() == '') {
    }
});

This solution is just to answer your question, and not to suggest as a good practice. You might at some point add further input boxes in the DOM, and you will have to keep adding to the :not list as everything else will be bound to the keyup event.

The better solution is to give the three input boxes a common class, and update your code like this:

$('input.class_of_txt').keyup(function () {

Add a common class to the 3 textboxes.

<input type="text" class="myclass" />

And then only reference that class:

$('.myclass').keyup(function () {

Any other solution will reduce the maintainability of your code. What if there will be another special textbox? You will make an exception for that also?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM