如何使用 JavaScript 单击一个 href 后编辑 3 个标签的值

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

本文关键字:编辑 标签 href JavaScript 何使用 单击 一个      更新时间:2023-09-26
我想

在单击单个 href
时使三个不同的标签可编辑我有:

<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>

将这个标签更改为:

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

如何为 2 个或更多标签执行此操作???例如关注标签...:

<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>

等等...

若要使不同的标签同时可编辑,单击单个链接时,可以使用 JQuery 的 each 函数,如以下代码所示:

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

希望对您有所帮助!