JQuery selector by form index and input name

Take the next page with two forms with different classes, but each form has an input with the same name.

<form class='first_form'>
 <input name='test' value='1' />
</form>
<form class='second_form'>
 <input name='test' value='3'/>
</form>

I can get the form index, and I know the input name, but I do not know the input index.

Is there a way to associate the selector with the form index and input name to get the value?

I tried chaining but nothing works

var inputName = 'test';
Var formIndex = 1;

$('*[name="' + inputName + '"]' +' ' + '$("form").eq(' + formIndex + ')').val();
+4
source share
5 answers

Fiddle

var formIndex=0;
var inputName="txtbox";
vall= $("form:eq("+ formIndex+") input[name= "+ inputName +" ]").val();
alert(vall);

your order was wrong

+3
source

Unconfirmed, but can you do:

$('form:nth-of-type(1) input[name="test"]').val();
+2
source
$("form:nth-child("+formIndex+") input[name='"+inputName+"']").val();
+2

:

var fieldName = 'test';
var formId = '.first_form'
$('form'+formId+' input[name='+fieldName+']).val()

, id class. ( 5, , :)) :)

- :

 var currentForm = $('form'+formId);
 currentForm//here you can put a log into console if element has not been found and find that bug sooner.
 currentForm.find('input[name='+fieldName+']').val()
+2

DOM, :

document.forms[formIndex]
document.forms[formName]

name, :

document.forms[formIndex][inputName]
document.forms[formName][inputName]

$(...), jQuery. :

var inputName = 'test',
    formIndex = 1;

$(document.forms[formIndex][inputName]);

, , , , .

, document.forms HTMLCollection HTMLFormElement . HTMLCollection HTMLFormElement .

+2

All Articles