【问题标题】:Jquery table edit row cancel button is not working properlyJquery 表编辑行取消按钮无法正常工作
【发布时间】:2018-05-20 04:51:28
【问题描述】:

我正在尝试编辑表格行但无法正常工作。 如果我单击编辑按钮,然后删除文本框值,然后单击显示警报的编辑按钮以填充文本框,否则 ucer 将单击取消按钮 previuse 我想要显示的值,但取消按钮无法正常工作。任何人都可以解决这个问题。代码见 JSFiddle:http://jsfiddle.net/9KEGd/191/

Js:

 $(function () {

$(".edit").click(function (e) {
    e.preventDefault();
    e.stopImmediatePropagation();
    var btn = $(this);
    var td = btn.closest("tr").find(".editable");

    var currentValue = td.text();

    //Save current text in td data attribute
    $(td).data("current-value", currentValue);

    if(btn.text() === "edit")
    {
        td.html("<input type='text' value="+currentValue+" />");
        btn.html("save");
    }
    else
    {
     if(td.find("input").val()==""){

    alert("please fill the text box")
    }else{
         td.html(td.find("input").val());
        btn.html("edit");
        }
    }



});

 $(".cancel").click(function (e) {
    e.preventDefault();  
    e.stopImmediatePropagation();
    var td = $(this).closest("tr").find(".editable");

    //Read data attribute to get saved text
    var currentValue = $(td).data("current-value");
    if(currentValue != "")
    {
        td.html(currentValue);
        $(this).parent().find(".edit").html("edit");

        //Set attribute to empty string
        $(td).data("current-value", "");
    }else{


    }
});
});

html:

  <table id="tabledata">
  <thead>
    <th>RecID</th>
    <th>Col1</th>
    <th>opt</th>
</thead>
<tr>
<td><a><div class="nestedtable">Tableshowing no need edit</div></a><span class="editable">RecID1</span></td>
    <td>Val1.1</td>
    <td>
    <ul>
    <li> <a class="edit">edit</a></li>
     <li> <a class="cancel">cancel</a></li> 
    </ul>


    </td>
</tr>
<tr>
<td><a><div class="nestedtable">Tableshowings no need edit</div></a><span class="editable">RecID2</span></td>
    <td>Val2.1</td>
    <td>    <ul>
    <li> <a class="edit">edit</a></li>
     <li> <a class="cancel">cancel</a></li> 
    </ul></td>
</tr>
<tr>
    <td><a><div class="nestedtable">Tableshowing no need edit</div></a><span class="editable">RecID3</span></td>
    <td>Val3.1</td>
    <td>    <ul>
    <li> <a class="edit">edit</a></li>
     <li> <a class="cancel">cancel</a></li> 
    </ul></td>
</tr>
</table>

【问题讨论】:

  • jsfiddle 对我来说很好
  • 我不太确定问题是什么,请您澄清一下吗?
  • 首先点击编辑按钮,然后从可编辑的文本框中删除文本,然后点击保存按钮警报将出现:请填写文本框“然后点击取消。看到什么都不会发生。点击后我想要那个取消按钮我想显示之前的值。如果文本框为空,则在单击编辑按钮后简单地告诉取消按钮不起作用
  • 我不想用空的文本框显示。如果单击取消按钮时它为空,我想在文本框中显示下一个 td 值

标签: javascript jquery


【解决方案1】:

我明白了你的问题。您需要在单击编辑时存储当前值,但在单击编辑和保存时存储它。这是一个更新的工作小提琴。 http://jsfiddle.net/9KEGd/193/

工作代码与小提琴相同:

$(function () {

$(".edit").click(function (e) {
    e.preventDefault();
    e.stopImmediatePropagation();
    var btn = $(this);
    var td = btn.closest("tr").find(".editable");



    //Save current text in td data attribute

    if(btn.text() === "edit")
    {
    //store the current value only on click of EDIT and not on save
      var currentValue = td.text();
      $(td).data("current-value", currentValue);
        td.html("<input type='text' value="+currentValue+" />");
        btn.html("save");
    }
    else
    {
     if(td.find("input").val()==""){

    alert("please fill the text box")
    }else{
         td.html(td.find("input").val());
        btn.html("edit");
        }
    }



});

 $(".cancel").click(function (e) {
    e.preventDefault();  
    e.stopImmediatePropagation();
    var td = $(this).closest("tr").find(".editable");

    //Read data attribute to get saved text
    var currentValue = $(td).data("current-value");
    if(currentValue != "")
    {
        td.html(currentValue);


        //Set attribute to empty string
        $(td).data("current-value", "");
    }else{


    }
    $(this).parents('tr').find(".edit").html("edit");
});

});

我还修复了单击取消时将 html 更改为 EDIT 的问题。

【讨论】:

  • 谢谢..现在它正在工作...我如何显示下一个 td 值而不是以前的值
猜你喜欢
  • 2021-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-07
相关资源
最近更新 更多