javascript - 如何用jquery检测哪个选择发生变化

标签 javascript jquery

我正在动态地创建一些选择,从ajax导入选项值和名称。 select 的 id 类似于 l1、l2、l(当前 select 的编号),select 的名称是其渐进编号。 我必须检测哪个选择更改,我需要 ID 或名称。 这是我的代码:

   $("#plus").click(function(){
                    $('#prod').append(
                        $('<div/>',{
                            'class' : 'row',
                            'id': 'r' + nplus
                        }).append(
                        $('<div/>',{
                            'class' : 'col',
                            'id': 'cl' + nplus
                        }).append(
                        $( '<select/>', {
                            'id':'l' + nplus,
                            'type': 'select',
                            'name': nplus
                        }))).append(
                        $('<div/>',{
                            'class' : 'col',
                            'id': 'cp' + nplus
                        }).append(
                        $( '<select/>', {
                            'id':'p' + nplus,
                            'type': 'select',
                            'name': nplus
                        }))).append(
                        $('<div/>',{
                            'class' : 'col',
                            'id': 'cq' + nplus
                        }).append(
                        $( '<input/>', {
                            'id':'q' + nplus,
                            'type': 'number',
                            'name': nplus
                        }))))
                    nplus++;
    });

为了检测哪些选择更改,我尝试了:

$("select[id^='l']").change(function(){
                    var nameselect = $(this).attr("name");
                });

你有什么想法吗?

最佳答案

如果您的选择输入有选项,它可能如下所示:-

let nplus=0;
 $("#plus").click(function()
 {
                    $('#prod').append(
                        $('<div/>',{
                            'class' : 'row',
                            'id': 'r' + nplus
                        }).append(
                        $('<div/>',{
                            'class' : 'col',
                            'id': 'cl' + nplus
                        }).append(
                        $( '<select/>', {
                            'id':'l' + nplus,
                            'type': 'select',
                            'name': nplus
                        }).append(new Option('option 1', 'opt1', false, false)).append(new Option('Option 2', 'opt2', false, false)))).append(
                        $('<div/>',{
                            'class' : 'col',
                            'id': 'cp' + nplus
                        }).append(
                        $( '<select/>', {
                            'id':'p' + nplus,
                            'type': 'select',
                            'name': nplus
                        }).append(new Option('Option 2.1', 'opt2.1', false, false)).append(new Option('Option 2.2', 'opt2.2', false, false)))).append(
                        $('<div/>',{
                            'class' : 'col',
                            'id': 'cq' + nplus
                        }).append(
                        $( '<input/>', {
                            'id':'q' + nplus,
                            'type': 'number',
                            'name': nplus
                        }))))
                    nplus++;
    });

$(document).on('change','select', function() 
{
        let eleID = this.getAttribute('id');
        let eleName = this.getAttribute('name');
         console.log(eleID);
         console.log(eleName);
});

尝试Example :

//检查控制台中所选元素的 id 和名称:-

关于javascript - 如何用jquery检测哪个选择发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60606391/

相关文章:

javascript - 基于预加载图像更新 src 图像不起作用

javascript - 用户向下滚动时的不同导航菜单

javascript - 计算两个坐标之间距离的函数

javascript - 上传总大小

javascript - R DT rowCallback 条件格式化每个单元格

javascript - Selectize.js:将类别标签添加到选择中

javascript - 创建一个不会被阻止的小书签

javascript - 将 Redux 映射状态 react 到 Prop 不起作用

javascript - 检查函数是否需要任何数据

javascript - 如何在调整大小时删除类?