阻止输入控件在点击时清空

时间:2013-05-24 12:28:16

标签: jquery asp.net textbox

我有asp:Listview我填充了数据。单击一行时,table的其中一个单元格将替换为asp:TextBox,以允许编辑所选值,并在文本框上设置焦点。到目前为止,非常好。

当我没有点击时,我可以按预期编辑该值。但是,当我再次单击input控件时,其内容会消失。

$(document).ready(function () {
    $('tr[id*="itemRow"]').click(function () {
        $clickedRow = $(this);
        var invNr = $clickedRow.find('span[id$="InvoiceNumber"]').text(),
            newInvNr,
            oldHtml = $clickedRow.find('span[id$="InvoiceNumber"]').html();
        $clickedRow.find('span[id$="InvoiceNumber"]').html('<asp:TextBox ID="editInvNr" runat="server"></asp:TextBox>');
        $clickedRow.find('input[id$="editInvNr"]').val(invNr).focus().focusout(function () {
            //capture new invoice nr
            newInvNr = $(this).val();
            //switch textbox back to span using new invoice nr
            $clickedRow.find('span[id$="InvoiceNumber"]').html(newInvNr);
            //check if anything changed, if so update it in the db
            if (newInvNr != invNr) {
                //update new value to db
            }
        });
    });
});

jsfiddle here中,我已使用常规html asp:TextBox元素替换了input的插入,结果完全相同。您可以单击最后一列中的“1”以查看我看到的内容。 我觉得我忽略了文本框控件的一些内置行为,如果我是,那么我为我缺乏知识而道歉。

无论如何,我想了解如何防止这种情况发生的一些指示。 非常感谢

1 个答案:

答案 0 :(得分:1)

在文本框中检测到单击时退出该功能:

if($clickedRow.find('input[id$="editInvNr"]').length > 0)
    return;