Someone33
Someone33

Reputation: 568

disable/ enable dynamically added input field by jQuery

problem 1:

The following code should enable/ disable an dynamically added input field by jquery, but it does not work as expected:

$(document).ready(function () {
    $('#auto').change(function () {
        if ($(this).val() == 1) {
            $('input[id="speed_car"]').prop('disabled', false).css('background', '').val('4')
        } else {
            $('input[id="speed_car"]').prop('disabled', true).css('background-color', '#03f').val('')
        }
    });
});

Problem2: If i save the field and reload the page it works, but only for the first < tr > (which contains other input fields and selects).

How should i change the code so that it will work correctly?

Here's part of the code which add's the row:

  var modell = <?php echo $modell; ?>;
   .
   .
   .
  html += '    <td class="left"><select name="car_modell[' + modell + '][auto]">'; 
  html += '     <option value="1" selected="selected"><?php echo $text_enabled; ?></option>';
  html += '     <option value="0"><?php echo $text_disabled; ?></option>';
  html += '    </select></td>';
  html += '    <td class="left"><input type="text" maxlength="1" name="car_modell[' + modell + '][speed_car]" value="4" size="3" /> </td>'; 
  ecc...

Thanks in advance for helping.

Upvotes: 0

Views: 1153

Answers (1)

Micah
Micah

Reputation: 313

You should use the "on" function. Look here: http://api.jquery.com/on/

jQuery Binds on Load, but the "on" causes it to evaluate/bind at that moment.

$("#dataTable tbody tr").on("click", function(event){
  alert($(this).text());
});

Upvotes: 1

Related Questions