0

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.

5
  • just wondering, is input id speed_car unique? Is #auto change fired as expected? Could you provide a jsfiddle BTW? Commented Jun 18, 2013 at 22:15
  • that's one of the problem. it's always the same id. But i don't know how to write it in another way Commented Jun 18, 2013 at 22:17
  • 1
    so, easy way is to not use ID but class instead. You can still apply some context if needed. And what about change event, is it fired? Commented Jun 18, 2013 at 22:20
  • As roasted says, use a class not an id. IDs must be unique. Commented Jun 18, 2013 at 22:21
  • id or class will be always the same if the row will be added. Doesn't change. The problem is that the row's are added dynamically. Commented Jun 18, 2013 at 22:24

1 Answer 1

1

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());
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.