jQuery selection with dynamic id's

后端 未结 5 1849
余生分开走
余生分开走 2020-12-15 09:56

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

相关标签:
5条回答
  • 2020-12-15 10:30

    I don't think you should be using the @ sign and you're missing some quotes:

    Instead of this: $('input[id^=@id_airline_for_]')

    try this: $("input[id^='id_airline_for_']")

    See this, too:

    http://docs.jquery.com/Selectors/attributeStartsWith#attributevalue

    0 讨论(0)
  • 2020-12-15 10:35
    $("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
    });
    
    0 讨论(0)
  • 2020-12-15 10:35

    The following will get you all of the inputs that contain id_airline_for in their name.

    $("input[id^='id_airline_for']")
    

    If you need to do this on a per form basis, you'll want to give each form its own ID and select them in groups that way.

    $("#formJetBlue input[id^='id_airline_for']")
    $("#formNwa input[id^='id_airline_for']")
    $("#formOceanic input[id^='id_airline_for']")
    
    0 讨论(0)
  • 2020-12-15 10:52

    Selects elements that have the specified attribute with a value containing the a given substring

    $('input[id *= id_airline_for]').attr('checked', headerChecked);
    

    it will select all elements that contain 'id_airline_for' string in the 'id' attribute

    0 讨论(0)
  • 2020-12-15 10:55

    You can use $("input#id_airline_for" + id) (where id is your number, e.g., 8), but it will be faster if you drop the tag name and just use:

    $("#id_airline_for" + id);
    
    0 讨论(0)
提交回复
热议问题