我的代码如下:
<select class='form-control' id='name_pipe1' name='name_pipe[]' onchange='check_database()' required><option>-- Choose Pipe Name --</option></select>
<select class='form-control' id='name_pipe2' name='name_pipe[]' onchange='check_database()' required><option>-- Choose Pipe Name --</option></select>
<select class='form-control' id='name_pipe3' name='name_pipe[]' onchange='check_database()' required><option>-- Choose Pipe Name --</option></select>
如何使用javascript一一获取ID以填充我的check_database?
答案 0 :(得分:1)
您可以将id作为参数传递给函数内部。例如:
<select id='name_pipe1' onchange="check_database('name_pipe1')" required><option>-- Choose Pipe Name --</option></select>
或者您可以在函数内部传递事件
<select id='name_pipe1' onchange='check_database(event)' required><option>-- Choose Pipe Name --</option></select>
,然后在您的函数中:
check_database(event){ const id = event.target.id }
答案 1 :(得分:0)
获取所有select.form-control
,然后检查其ID:
const my_select_list = document.querySelectorAll("select.form-control");
for (let i = 0; i < my_select_list.length; i++) {
my_id = my_select_list[i].id;
};
然后使用ID执行您想要的操作。
这能回答您的问题吗?
答案 2 :(得分:0)
在check_database()
中,您应该有权访问this
对象,该对象将引用触发该函数的当前元素。
因此,您可以在this.id
内执行类似check_database
的操作来获取更改后的选择的ID。