-1

This won't work:

    <input type="text" name="start_time[0][0]" value="00">
    <input type="text" name="start_time[0][1]" value="01">
    <input type="text" name="start_time[1][0]" value="10">
    <input type="text" name="start_time[1][1]" value="11">

    $("form[name='fare-form'] input[name='start_time[][]'").click(function() {
        console.log($(this).val());
    });

If I specify array indices, it'll work. And I'm presuming using 1-dimensional array works which I've used elsewhere.

Why won't it work?

I learnt it has to match exactly. So in the case where I do 1-dimensional array such as:

<input type="text" name="start_time[]" value="1">
<input type="text" name="start_time[]" value="2">

$("form[name='fare-form'] input[name='start_time[]'").click(function() {
    console.log($(this).val());
});

This works because Javascript or whatever will automatically create array of two indices but the reference of start_time[] in event will point to both elements of array?

Ben Kao
  • 621
  • 3
  • 8
  • 18
  • 2
    None of those names have `[][]` in them. All the `[]` have numbers in them – Taplar Jul 17 '19 at 16:59
  • 3
    Give all of them a class of `start_time` and select by the class. done. – Taplar Jul 17 '19 at 17:00
  • I used class. But out of curiousity, why doesn't it work? Care to explain? – Ben Kao Jul 17 '19 at 17:01
  • Related: https://stackoverflow.com/questions/33615334/jquery-finding-partial-class-name/33615377#33615377 – Taplar Jul 17 '19 at 17:07
  • 1
    It doesn't work because the value you place in the selector, ie. `input[name='start_time[]']` need to *exactly* match the attribute on the element. In your case, the first `input` would be `input[name='start_time[0][0]']`. Also note that your attribute selector in both examples is missing a closing `]` – Rory McCrossan Jul 17 '19 at 17:09

1 Answers1

3

When you use [name=...], the name has to match exactly, it doesn't do any kind of pattern matching. start_time[][] is not an exact match for start_time[0][0] or any of the other names in the form.

You can use input[name^=start_time] to match any name that begins with start_time.

See jQuery Attribute selectors for all the different ways you can match attributes in jQuery selectors. There's no form that will do the kind of pattern matching you're trying to do.

Barmar
  • 741,623
  • 53
  • 500
  • 612