我有一个动态生成的表单,并且动态生成了id(和潜在的类).这些表格是一样的,但是他们有相关的ID到底.
如何选择每一组输入并对每个输入应用代码?
我正在尝试$(‘input [id ^ = @ id_airline_for_]’),但无法让它飞.我怀疑我遗漏了一些基本的jQuery知识,因为我确信这是动态表单的常见问题.
<form method='POST'> <label for="id_airline_for_8">Airline</label>: <input id="id_airline_for_8" class="arrival_transfer_toggle_8" type="text" maxlength="30" name="airline_for_8"/> <label for="id_flight_number_for_8">Flight Number</label>: <input id="id_flight_number_for_8" class="arrival_transfer_toggle_8" type="text" maxlength="30" name="flight_number_for_8"/> <label for="id_airline_for_17">Airline</label>: <input id="id_airline_for_17" class="arrival_transfer_toggle_17" type="text" maxlength="30" name="airline_for_17"/> <label for="id_flight_number_for_17">Flight Number</label>: <input id="id_flight_number_for_17" class="arrival_transfer_toggle_17" type="text" maxlength="30" name="flight_number_for_17"/> -- snip -- </form>
编辑:我应该更新,我想要能够执行一些动作,当输入被点击,但只有最后匹配的id的类.
为了方便起见,让我们说我希望在#id结尾处有一个匹配的id的所有输入消失,一旦被点击(仅供参考).
解决方法
$("input[id^='id_airline_for_']").each(function() { var id = parseInt(this.id.replace("id_airline_for_",""),10); var airline = $("#id_airline_for_" + id); var flightNumber = $("#id_flight_number_for_" + id); // do stuff with airline and flightNumber <input>s });