具有动态id的jQuery选择 [英] jQuery selection with dynamic id's

查看:92
本文介绍了具有动态id的jQuery选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个动态生成的表单,并且具有动态生成的id(以及可能的类)。表格是相同的,但它们的相关ID已添加到最后。

I have a form that is dynamically generated, and has dynamically generated id's (and potentially classes). The forms are the same but they have the related id tacked on to the end.

如何选择每组输入并将代码应用于每一个?

How can I select each set of inputs and apply code to each one?

我正在尝试使用$('input [id ^ = @ id_airline_for_]'),但无法让它飞起来。我怀疑我遗漏了一些基本的jQuery知识,这些知识阻碍了我,因为我确信这是动态表单的常见问题。

I was experimenting with $('input[id^=@id_airline_for_]') but could not get it to fly. I suspect I am missing some fundamental jQuery knowledge that is holding me back since I am sure this is a common problem with dynamic forms.

<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的类。

I should update that i want to be able to perform some action when the input is clicked but only for classes with a matching id at the end.

为了简单起见,我想说我希望#id末尾带有匹配id的所有输入都消失当点击一个时(仅为了参数)。

To make it easy, lets say I want all inputs with a matching id at the end of the #id to disappear when one is clicked (just for arguments sake).

推荐答案

$("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
});

这篇关于具有动态id的jQuery选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆