javascript - 根据表 td 输入更改标签?

标签 javascript jquery html save label

让我首先解释一下为什么我需要这样做,这样它才有意义。页面上有各个部分的各种标签/标题。用户可能希望将这些标题命名为其他名称或使用不同的语言。可以用“色调”代替“颜色”。有一个表保存标签信息和部分信息。所以,“颜色”和“红色”等等。

我需要的是当用户更改表中的标签输入字段并单击“保存”时,页面上的相应标签将发生更改。在表中,第一列是匹配标签的 id 以及相应输入的类。 http://jsfiddle.net/NNpCB/4/

jQuery

// dynamically give table text inputs, with correct label classes
var valueCol = $("table#ruleTable tr td:nth-child(2)");
valueCol.html(function(){
    return '<input value="' + $(this).text() + '" class="' + $(this).prev().text() + '" />';
});

// save new label text
$('.saveLbl').click(function () {
    // for each input that was changed, find the corresponding label(s) and change the label text
    // the input .class matches the label #id
});

HTML

<label id="lblcolor">Colors</label>
<ul>
    <li>Red</li>
    <li>Blue</li>
    <li>Yellow</li>
</ul>    

<label id="lblshape">Shapes</label>
<ul>
    <li>Square</li>
    <li>Circle</li>
    <li>Rectangle</li>
</ul> 
<br /><br />
<table id="ruleTable" border='1' cellpadding='15'>
    <thead>
        <tr>
            <th>Label</th>
            <th>Display Value</th>
            <th>Language</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>lblcolor</td>
            <td>Colors</td>
            <td>ENG</td>
        </tr>
        <tr>
            <td>lblshape</td>
            <td>Shapes</td>
            <td>ENG</td>
        </tr>
    </tbody>        
</table>
<br /><br />
<button class='saveLbl'>Save</button>

最佳答案

尝试以下代码:( http://jsfiddle.net/W4k7W/ )

 $('.saveLbl').click(function () {
    // for each input that was changed, find the corresponding label and change the label text
    // the input .class matches the label #id
    var rows=$("#ruleTable tbody").children();
    for(var i=0;i<rows.length;i++){
        var rowKids = $(rows[i]).children();
        var labelClass=$(rowKids[0]).text();
        var value=$($(rowKids[1]).children()[0]).val(); // <--- rowKids[1] is the td , its first child is the input row 
        $("#"+labelClass).text(value);
    }
});

关于javascript - 根据表 td 输入更改标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24192306/

相关文章:

html - 如何将忘记链接放入输入密码框中?

javascript - 包装 JavaScript 函数引用

jquery - 为什么 jquery ui 自动完成功能放弃对这些功能的支持?

javascript - 单击另一个子菜单时如何关闭一个子菜单

javascript - 如何显示带有被单击元素位置的警报?

javascript - 带有创建元素的appendChild,删除创建的临时元素

javascript - 为什么仅调用同步函数时 javascript promises 是异步的?

javascript - Mathjax 在处理时隐藏表达式

javascript - 如何访问包装 Angular 2 observable 的构造函数的范围?

javascript - 想要使用减号图标关闭侧面板