简体   繁体   中英

how to edit value of 3 labels after clicking on a href with javascript?

I want to make three different labels editable when I click on a single href
I have:

<tr>
    <td align="left">PLZ:</td>

    <td>
        <label for="name" class="control-label">
            <p class="text-info">
                <label style="color: #662819;" id="plz"></label>
                <i class="icon-star"></i>
            </p>
        </label>
        <input type="text" class="edit-input" />

        <div class="controls">
            <a class="edit" href="#">Daten sichern</a>
        </div>
    </td>
</tr>

changing this one label with:

CSS:

.edit-input {
    display:none;
}

JQuery:

$(function() {
    $('a.edit').on("click", function(e) {
        e.preventDefault();
        var dad = $(this).parent().parent();
        var lbl = dad.find('label');
        lbl.hide();
        dad.find('input[type="text"]').val(lbl.text()).show().focus();
    });

    $('input[type=text]').focusout(function() {
        var dad = $(this).parent();
        $(this).hide();
        dad.find('label').text(this.value).show();
    });
});

how to do it for 2 or more labels??? like for follow ones...:

<tr>
      <td align="left">Strasse:</td>
      <td>
        <label for="name" class="control-label">
            <p class="text-info">
                <label style="color: #662819;" id="street"></label>
                <i class="icon-star"></i>
            </p>
        </label>
        <input type="text" class="edit-input" />  
      </td>
</tr>

and so on...

To make different labels editable at the same time, when you click a single link, you can use JQuery's each function, as shown in the following code:

$('a.edit').on('click',function(e){
    e.preventDefault();

    //Search for .control-label items
    $('label.control-label').each(function(key,item){
        var label = $(item);          
        label.hide();
        label.siblings('input.edit-input').val(label.text()).show().focus();
    });

});

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM