Domanda

I have a table with a variable amount of rows like this:

<tr class"hotel-room">    
    <td class="cell1">
        <input type="hidden" class="hotel-id" name="input_01[label][row][progressive][id]" value="5893" />
        <input type="hidden" class="num-nights" name="input_01[label][row][progressive][nights]" value="2" />
        <select class="room-selection" name="input_01[label][row][progressive][]">
           <option value="etc"...>...options...</option>
        </select>
    </td>
    <td class="cell2">
        <input type="number" class="room-qty-input" name="input_01[label][row][progressive][]" />
    </td>
    <td class="cell3">
        <input type="text" class="room-price" name="input_01[label][row][progressive][]" />
    </td>
    <td class="buttons">
        stuff not relevant (two imgs, some text, more markup...)
    </td>
</tr>

While I'm inside 'room-qty-input' number input field, I retrieve the parent tr of the container td using var hotelRoom = $(this).closest('tr');

while I'm a function where I passed the 'tr' above, I need to get the following:

  • the value of the second hidden field in the first cell with class="num-nights"
  • the value of the number input field with class="num-qty-input"

These are supposed to be integers but they return as NaN. I tried to determine their value using:

  • var numbRooms = $(hotelRoom).find('input[type="number"]');

or

  • var numbNights = $(hotelRoom).find('.num-nights').val();

But they don't seem to return the correct value. This is within a bit messy function, perhaps I'm doing something else wrong. Would like to know if you have better methods for retrieving the data I'm looking for.

È stato utile?

Soluzione

Plugging what you said you did into jsbin, I can't see anything wrong.

$(document).ready(function () {
    $("input.room-qty-input")
        .on('focus', function () {
            var hotelRoom = $(this).closest('tr');
            var numbNights = hotelRoom.find('.num-nights').val();
            if (isNaN(numbNights)) console.log('NaN'); else console.log('is number');
            console.log(this, hotelRoom, numbNights);
    });
});
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top