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

前端 未结 2 727
清酒与你
清酒与你 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条回答
  •  Happy的楠姐
    2021-01-12 08:26

    First of all, id attribute cannot contains [ or ] character.

    There is lots of ways to get jQuery/plain JavaScript references to these elements. You can use descendant selector:

    $("#list-of-fields input"); document.getElementById("list....").getElementsByTagName("input");

    You can also use attribute selector:

    $("input[name^=field]");
    

    I'm not sure whether that's the only way but I think in plain JavaScript you'll have to fetch all input elements (document.getElementsByTagName) and then loop through array of these elements and check each element (whether it has name attribute which value starts with field).

提交回复
热议问题