How to edit data onclick

后端 未结 6 1333
庸人自扰
庸人自扰 2020-12-28 20:47

I am not sure what it is called, but I want to be able to click on e.g. a div containing a number, and then it will change into a input text field, with the val

6条回答
  •  醉梦人生
    2020-12-28 21:11

    You can do the following:

    Have a click event and create a textbox on the fly which takes the text inside the div as a value.

    The have a blur even which binds to that textbox and makes an AJAX call and in its success change the div text

    Lets say your HTML is like:

    Amazing Spider man

    And your JS code will be like:

    $('#fullname').click(function(){
        var name = $(this).text();
        $(this).html('');
        $('')
            .attr({
                'type': 'text',
                'name': 'fname',
                'id': 'txt_fullname',
                'size': '30',
                'value': name
            })
            .appendTo('#fullname');
        $('#txt_fullname').focus();
    });
    
    $(document).on('blur','#txt_fullname', function(){
        var name = $(this).val();
        $.ajax({
          type: 'post',
          url: 'change-name.xhr?name=' + name,
          success: function(){
            $('#fullname').text(name);
          }
        });
    });
    

    This is demostrated in this jsfiddle

提交回复
热议问题