How to get clicked element from multiple element with the same name different index index of array

Do Thanh Dat

I have a problem that. I have list of select option with same name but different index. I don't know how to detect element when I click into one of them. Example:

<select name='contract_sides[0][joint_venture]'>
</select>
<select name='contract_sides[0][company_id]'>
</select>

<select name='contract_sides[1][joint_venture]'>
</select>
<select name='contract_sides[1][company_id]'>
</select>
...........
<select name='contract_sides[n][joint_venture]'>
</select>
<select name='contract_sides[n][company_id]'>
</select>

When I change value one of them. I will show/hide <select name="contract_sides[index]company_id"></select> below. Thank you so much for your help. Sorry for my english ! P/s: The problem is simpler if we use the class but the problem is that we can only use the name.

Solution from Light:

$('select[name*="joint_venture"]').on('change', function () {    
  let name = $(this).attr('name');
  let index = name.match(/\[([0-9]*)\]/)[1];

  // Hide the company id
  $(`select[name="contract_sides[${index}][company_id]"]`).hide();
});
Shiny
$('select[name*="contract_sides"]').on('click', function () {
    // Your clicked element is now $(this)    
    let $this = $(this);

    // Name of the clicked element
    let name = $this.attr('name');

    // Index of the clicked element
    let index = name.match(/\[([0-9]*)\]/)[1];
});

Following your comment to another answer, I understand more what you were looking for, and I believe this should behave how you want

$('select[name*="joint_venture"]').on('change', function () {    
  let name = $(this).attr('name');
  let index = name.match(/\[([0-9]*)\]/)[1];

  // Hide the company id
  $(`select[name="contract_sides[${index}][company_id]"]`).hide();
});

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How to get index of name attribute from same name of multiple elements.

분류에서Dev

How to get index of element in Set object

분류에서Dev

Dart: get index of element in list 2 (element from list 1)

분류에서Dev

Finding an index of an element in a string array

분류에서Dev

how to print out the same name of an array element once

분류에서Dev

How to find the index of element in two dimensional hash

분류에서Dev

How do you access multi-dimension array by N array of index element-wise?

분류에서Dev

How to get multiple node element types into one variable from XML

분류에서Dev

Get position of clicked element with jquery?

분류에서Dev

PHP get a value from array index

분류에서Dev

Deserialize XML to object array with different name to XML element

분류에서Dev

jQuery overall index from parent element with specific class

분류에서Dev

Adding multiple returned MySQL rows to the same array key element in PHP

분류에서Dev

Windows forms container : Panel, how to access an element at a particular index?

분류에서Dev

How to get the nth element of an array in javascript?

분류에서Dev

Get non-repeating index from array fails on 0

분류에서Dev

Insert an element into a List at index 0: "Cannot convert"

분류에서Dev

Insert an element into a List at index 0: "Cannot convert"

분류에서Dev

Getting the index of a parent element of an event target

분류에서Dev

Fastest way to index an element in a sorted list in python?

분류에서Dev

How to place a spinner in whichever HTML element was clicked

분류에서Dev

How to get JSON response array all index values?

분류에서Dev

How to get multiple XML elements that are descendants of another element

분류에서Dev

Give a Index Name to associative array codeigniter

분류에서Dev

Push values to the same index of an array in PHP

분류에서Dev

jQuery each running multiple times on same element

분류에서Dev

XSD multiple elements with same name but different types

분류에서Dev

zip multiple files with the same name but different extensions

분류에서Dev

Get table view cell index once a button is clicked

Related 관련 기사

  1. 1

    How to get index of name attribute from same name of multiple elements.

  2. 2

    How to get index of element in Set object

  3. 3

    Dart: get index of element in list 2 (element from list 1)

  4. 4

    Finding an index of an element in a string array

  5. 5

    how to print out the same name of an array element once

  6. 6

    How to find the index of element in two dimensional hash

  7. 7

    How do you access multi-dimension array by N array of index element-wise?

  8. 8

    How to get multiple node element types into one variable from XML

  9. 9

    Get position of clicked element with jquery?

  10. 10

    PHP get a value from array index

  11. 11

    Deserialize XML to object array with different name to XML element

  12. 12

    jQuery overall index from parent element with specific class

  13. 13

    Adding multiple returned MySQL rows to the same array key element in PHP

  14. 14

    Windows forms container : Panel, how to access an element at a particular index?

  15. 15

    How to get the nth element of an array in javascript?

  16. 16

    Get non-repeating index from array fails on 0

  17. 17

    Insert an element into a List at index 0: "Cannot convert"

  18. 18

    Insert an element into a List at index 0: "Cannot convert"

  19. 19

    Getting the index of a parent element of an event target

  20. 20

    Fastest way to index an element in a sorted list in python?

  21. 21

    How to place a spinner in whichever HTML element was clicked

  22. 22

    How to get JSON response array all index values?

  23. 23

    How to get multiple XML elements that are descendants of another element

  24. 24

    Give a Index Name to associative array codeigniter

  25. 25

    Push values to the same index of an array in PHP

  26. 26

    jQuery each running multiple times on same element

  27. 27

    XSD multiple elements with same name but different types

  28. 28

    zip multiple files with the same name but different extensions

  29. 29

    Get table view cell index once a button is clicked

뜨겁다태그

보관