سؤال

I have following html:

<table>
  ....
  <tr>
    ....
  </tr> 
  ....
  <tr id="myid">
     <....>
        <input value="..." />
     </....>
  </tr>
  <tr id="myanotherid">
     <....>
        <input value="..." />
     </....>
  </tr>
</table>

How can I set the focus on this input field in table row "myid" with jquery ? I can't set id on input field, because this html was generated automatically.

هل كانت مفيدة؟

المحلول

Use the table as an anchor and find the input regardless of its value.

$('table tr#myid').find('input:first').focus()

نصائح أخرى

Selecting the field as-is:

$('input[value="abc"]').focus();

Or if there's a chance that there will be more than one input with that value:

$('input[value="abc"]:first').focus();

If it's the ID you need to focus on, use that in your selector:

$('#myid input:first').focus();

more jQuery selectors

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top