Как определить с помощью jquery, что выбрать изменение - PullRequest
0 голосов
/ 09 марта 2020

Я динамически создаю некоторые select, импортирую значения параметров и имена из ajax. Идентификатор выбора подобен l1, l2, l (номер текущего выбора), имя выбора - его прогрессивный номер. Я должен определить, какое выбрать изменение, мне нужен идентификатор или имя. Это мой код:

   $("#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");
                });

У вас есть идеи?

1 Ответ

1 голос
/ 09 марта 2020

если у вас есть опции для выбранных входов, это может выглядеть так: -

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);
});

Попробуйте Пример :

// проверка консоли на идентификатор и имя выбранного элемента: -

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...