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();
});
$('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] 삭제
몇 마디 만하겠습니다