Change Label to Textbox on edit hyperlink click

前端 未结 5 1048
花落未央
花落未央 2020-12-05 09:01

I am new to ruby on rails and twitter bootstrap. Accept my apologize if my question sound dumb. I am using twitter bootstrap for my site design. I have been trying to use b

相关标签:
5条回答
  • 2020-12-05 09:13

    As Chris said, Bootstrap does not provide this functionality as it is a front-end framework. you can use jQuery to achieve this. Though, you'll need to add some custom id's or classes to your elements to make sure you are only selecting the required elements. You can do something like the following:

    HTML

    <div class="control-group">
        <label for="name" class="control-label">
            <p class="text-info">Saghir<i class="icon-star"></i></p>
        </label>
        <input type="text" class="edit-input" />
        <div class="controls">
            <a class="edit" href="#">Edit</a>
        </div>
    </div>
    

    jQuery

    $(document).ready(function() {
        $('a.edit').click(function () {
            var dad = $(this).parent().parent();
            dad.find('label').hide();
            dad.find('input[type="text"]').show().focus();
        });
    
        $('input[type=text]').focusout(function() {
            var dad = $(this).parent();
            $(this).hide();
            dad.find('label').show();
        });
    });
    

    CSS

    .edit-input {
        display:none;
    }
    

    Here is a working JSFiddle for your reference.

    0 讨论(0)
  • I think you need jQuery for it. Try this :

    $('#edit').click(function() {
     var text = $('.text-info').text();
     var input = $('<input id="attribute" type="text" value="' + text + '" />')
     $('.text-info').text('').append(input);
     input.select();
    
     input.blur(function() {
       var text = $('#attribute').val();
       $('#attribute').parent().text(text);
       $('#attribute').remove();
     });
    });
    .control-label .text-info { display:inline-block; }
     
    <label for="name" class="control-label"><p class="text-info">Saghir</p><i class="icon-star"></i></label>
    <div class="controls">
       <a href="#" id="edit" class="btn">Edit</a>
    </div>
    <script src="//code.jquery.com/jquery-2.1.1.min.js"></script>

    Update

    If you want change label to input and text of label put into placeholder of input, try this for

    $('#edit').click(function() {
     var text = $('.text-info').text();
     var input = $('<input type="text" placeholder="' + text + '" />')
     $('.text-info').text('').append(input);
    });
    .control-label .text-info { display:inline-block; }
    <label for="name" class="control-label"><p class="text-info">Saghir</p><i class="icon-star"></i></label>
    <div class="controls">
       <a href="#" id="edit" class="btn">Edit</a>
    </div>
    <script src="//code.jquery.com/jquery-2.1.1.min.js"></script>

    jsfiddle jsfiddle 2

    0 讨论(0)
  • 2020-12-05 09:15

    I know its a old question, But just added Update option to Saghirs answer ,

    http://jsfiddle.net/integerz/k3p4vhnb/

        function edit(element) {
            var parent = $(element).parent().parent();
            var placeholder = $(parent).find('.text-info').text();
            //hide label
            $(parent).find('label').hide();
           //show input, set placeholder
            var input = $(parent).find('input[type="text"]');
            var edit = $(parent).find('.controls-edit');
            var update = $(parent).find('.controls-update');
            $(input).show();
            $(edit).hide();
            $(update).show();
            //$(input).attr('placeholder', placeholder);
        }
    
        function update(element) {
            var parent = $(element).parent().parent();
            var placeholder = $(parent).find('.text-info').text();
            //hide label
            $(parent).find('label').show();
            //show input, set placeholder
            var input = $(parent).find('input[type="text"]');
            var edit = $(parent).find('.controls-edit');
            var update = $(parent).find('.controls-update');
            $(input).hide();
            $(edit).show();
            $(update).hide();
        //$(input).attr('placeholder', placeholder);
        $(parent).find('label').text($(input).val());
    }
    

    This will enable the user to get back to the normal mode. Also the Ajax call for update can be triggered on update function.

    0 讨论(0)
  • 2020-12-05 09:26

    Use a hidden input

    <div class="control-group">
        <label for="name" class="control-label"><p class="text-info">Saghir<i class="icon-star"></i></p></label>
        <input type="text" class="input-medium" style="display:none;">
        <div class="controls">
            <a href="#" onclick="edit(this);">Edit</a>
        </div>
    </div>
    

    When user click on "Edit", grab the text from text-info (eg Saghir), hide the label, show the input and set the inputs placeholder-attribute.

    function edit(element) {
        var parent=$(element).parent().parent();
        var placeholder=$(parent).find('.text-info').text();
        //hide label
        $(parent).find('label').hide();
        //show input, set placeholder
        var input=$(parent).find('input[type="text"]');
        $(input).show();
        $(input).attr('placeholder', placeholder);
    }
    

    working fiddle : http://jsfiddle.net/TKW74/

    0 讨论(0)
  • 2020-12-05 09:27

    I fiddled a litte more with Amyth's answer. This one just lets you click the text to edit it. You just need to add a class to any text element to enable the functionality. Enter key updates the text. Click outside the input to abort.

    HTML:

    <span class="edit-on-click">Click to edit</span>
    

    JS:

    $(document).ready(function() {
      $('.edit-on-click').click(function() {
        var $text = $(this),
          $input = $('<input type="text" />')
    
        $text.hide()
          .after($input);
    
        $input.val($text.html()).show().focus()
          .keypress(function(e) {
            var key = e.which
            if (key == 13) // enter key
            {
              $input.hide();
              $text.html($input.val())
                .show();
              return false;
            }
          })
          .focusout(function() {
            $input.hide();
            $text.show();
          })
      });
    });
    

    JSFiddle

    Update (implementing AJAX to update db value):

    Here's how I ended up using it in practice: this uses data attributes on the original element for id, model, and field to update in an ajax request. (This is from a Django app using Bootstrap, so some lines might be obsolete for you depending on your setup.)

    HTML:

    <span onclick="editOnClick(this)" 
          data-id="123" 
          data-model="myapp.MyModel" 
          data-field="my_field">Click to edit</span>
    

    JS:

    function editOnClick(el) {
        var $text = $(el),
                $input = $('<input type="text" />');
    
        $text.hide()
                .after($input);
    
        $input.val($.trim($text.html())).show()
                .tooltip({title:"press ENTER to save<br>click outside to cancel", container:"body", trigger:'focus', html:true})
                .focus()
                .keypress(function(e) {
                    var key = e.which;
                    if (key == 13) // enter key
                    {
                        $.ajax({
                            type: "POST",
                            data: {
                                value:$input.val(),
                                obj_id:$text.attr('data-id'),
                                obj_model:$text.attr('data-model'),
                                obj_field:$text.attr('data-field'),
                                csrfmiddlewaretoken:'{{ csrf_token }}'
                            },
                            url: "{% url 'edit_on_click' %}",
                            cache: false,
                            dataType: "html",
                            success: function(html, textStatus) {
                                $input.hide();
                                $text.html(html)
                                        .show();
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                alert('Error: ' + XMLHttpRequest.responseText)
                            }
                        });
    
                        return false;
                    }
                })
                .focusout(function() {
                    $input.hide();
                    $text.show();
                })
    }
    
    0 讨论(0)
提交回复
热议问题