Slimshadddyyy
Slimshadddyyy

Reputation: 4071

jQuery Disable checkbox with same value

So this has to be the easiest question of the day. I have below HTML structure where all input checkbox values must be unique.

    <tr>
        <td>
        <input type="checkbox" value="14292" name="chec_emp[]" class="chec_emp">
        </td>
        <td>Test User One</td>
        <td>Autobot</td>
        <td>Shadow</td>
        <td>50</td>
    </tr>

 <tr>
        <td>
        <input type="checkbox" value="14293" name="chec_emp[]" class="chec_emp">
        </td>
        <td>Test User One</td>
        <td>Autobot</td>
        <td>Billed</td>
        <td>50</td>
    </tr>


    <tr>
        <td>
        <input type="checkbox" value="14292" name="chec_emp[]" class="chec_emp">
        </td>
        <td>Test User Two</td>
        <td>Autobot</td>
        <td>Billed</td>
        <td>50</td>
    </tr>

The value 14292 is generated twice here.

  1. How can I disable checkbox with duplicate value
  2. How can I disable checkbox with <td>Shadow</td>

Upvotes: 0

Views: 544

Answers (1)

Brewal
Brewal

Reputation: 8189

If you want to add the disabled attribute to the next occurrences of checkboxes with the same value :

$('.chec_emp').each(function(){
    if ($(this).is(':enabled')) {
        $('.chec_emp[value="'+$(this).val()+'"]').not(this).prop('disabled', 'disabled');
    }
});

demo

Upvotes: 3

Related Questions