Accessing an array of HTML input text boxes using jQuery or plain Javascript

前端 未结 2 709
清酒与你
清酒与你 2021-01-12 07:50

I\'m looking to create a form which contains a dynamic number of input text boxes. I would like each text box to form part of an array (this would in theory make it easier f

2条回答
  •  清酒与你
    2021-01-12 08:29

    Give each element a class and access the group using jQuery:

    Field 1:

    Field 2:

    jQuery:

    $("input.fields").each(function (index)
    {
        // Your code here
    });
    

    This will run the anonymous function on each input element with a classname of "fields", with the this keyword pointing to the current element. See http://api.jquery.com/each/ for more info.

提交回复
热议问题