Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 当我们单击动态创建的编辑图标时,如何更改输入字段中的值?_Javascript_Jquery_Html - Fatal编程技术网

Javascript 当我们单击动态创建的编辑图标时,如何更改输入字段中的值?

Javascript 当我们单击动态创建的编辑图标时,如何更改输入字段中的值?,javascript,jquery,html,Javascript,Jquery,Html,这里的代码中有ajax,它将值分配给字段,但有一个动态创建的编辑图标,这意味着您在数据库中添加了多少地址,然后将创建多少次编辑图标。我需要的是,当我点击第一个按钮时,它会提醒其id值,当我点击另一个按钮时,它会提醒其id值 以下是我尝试的代码:- var full_url = document.URL; // Get current url var url_array = full_url.split('=') // Split the string into an array with / a

这里的代码中有ajax,它将值分配给字段,但有一个动态创建的编辑图标,这意味着您在数据库中添加了多少地址,然后将创建多少次编辑图标。我需要的是,当我点击第一个按钮时,它会提醒其id值,当我点击另一个按钮时,它会提醒其id值 以下是我尝试的代码:-

var full_url = document.URL; // Get current url
var url_array = full_url.split('=') // Split the string into an array with / as separator
var UserId = url_array[url_array.length-1];  // Get the last part of the array (-1)
$.ajax({
    url:"url,
    type: "GET",
    dataType: 'json',
    async: false,
    data:{"UserId":UserId},
    success: function(response){
        if (response.response.total_record[0].status === "active") {
            $('#email').html(response.response.total_record[0].email);
            $('#name').html(response.response.total_record[0].first_name+" "+response.response.total_record[0].last_name);
            $('#first').val(response.response.total_record[0].first_name);
            $('#last').val(response.response.total_record[0].last_name);
            $('#phone').val(response.response.total_record[0].phone_number);
            $('#alternative').val(response.response.total_record[0].alternative_number);
            $('#id').val(response.response.total_record[0]._id);
            $('#status').val(response.response.total_record[0].status)
            if (response.response.total_record[0].status === "active") {
                $('#activate').hide();
            }
            if (response.response.total_record[0].status === "deactivate") {                    $('#activate').show();
                $("#deactivate").hide();
            }
            $.each(response.response.total_record[0].address,function(i,item){
                console.log(response.response.total_record[0].address[i])
                $('#edit_id').val(response.response.total_record[0].address[i]._id)
            $('.cards').append('<div class="location-list"><header class="header_title"><div class="location_heading"><h3>Location:</h3></div><div class="edit_icon"><a class="editByAnchor" id='+response.response.total_record[0].address[i]._id+' href="#" data-toggle="modal" data-target="#edit_address"><i class="fa fa-edit"></i></a></div></header><div id="dAddress" class="location-detial"><p><span id='+response.response.total_record[0].address[i]._id+'>'+response.response.total_record[0].address[i].address+'</span></p></div></div>');
            });                 
        }
    }
});  
产生输出

<a class="editByAnchor" id="1" href="#" data-toggle="modal" data-target="#edit_address"><i class="fa fa-edit"></i></a>
/*more like this but id will be change base on the dynamically fields*/

/*更像这样,但id将根据字段进行更改*/

上面的输出
锚定标记
具有
id
属性,它将在您看到我的代码时动态分配,因此如何通过单击它们来获得每个图标的
id
属性。

对于动态创建的元素,请使用
on()
。您还必须使用
单击
事件,而不是
更改

$('.editByAnchor').on('click', function() {
    alert("You just clicked checkbox with the name " + this.id)
});

通过引用我发布了一个答案,一切都很好,我必须通过以下方式更改我的代码

$(".editByAnchor").click(function(){
    var id = this.id;
    alert(id)
});

是的,这也行谢谢你宝贵的回答@Mamun
$('.editByAnchor').on('click', function() {
    alert("You just clicked checkbox with the name " + this.id)
});
$(".editByAnchor").click(function(){
    var id = this.id;
    alert(id)
});