使用 Jquery 的空文本框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2083923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 12:43:52  来源:igfitidea点击:

Empty textbox using Jquery

jquerytextbox

提问by Anand Rockzz

I'm able to hide the 'tr' when 'Remove' is clicked. with the following code.

单击“删除”时,我可以隐藏“tr”。使用以下代码。

$("a#minus").bind("click", function(e){
    $(this).closest('tr').hide();
});

But I also want to clear the content of the 2 text boxes (id of the textbox's are dynamic [frm_Expense_expensesVO___strAmount and frm_Expense_expensesVO___memo] here '*' goes from 1 to infinity). Please help. Thanks

但我也想清除 2 个文本框的内容(文本框的 id 是动态的 [frm_Expense_expensesVO_ __strAmount 和 frm_Expense_expensesVO___memo] 此处“*”从 1 到无穷大)。请帮忙。谢谢

<table>
<tr>
    <td>
        Amount
    </td>
    <td>
        Memo
    </td>
    <td>    
        &nbsp;
    </td>
</tr>
<tr>
    <td>
        <input type="text" name="expensesVO[0].strAmount" value="2.30" id="frm_Expense_expensesVO_0__strAmount"/>
    </td>
    <td>
        <input type="text" name="expensesVO[0].memo" value="Five" id="frm_Expense_expensesVO_0__memo"/>
    </td>
    <td>
        <a id="minus" href="#">Remove</a>
    </td>
</tr>
<tr>
    <td>
        <input type="text" name="expensesVO[1].strAmount" value="3.45" id="frm_Expense_expensesVO_1__strAmount"/>
    </td>
    <td>
        <input type="text" name="expensesVO[1].memo" value="Six" id="frm_Expense_expensesVO_1__memo"/>
    </td>
    <td>
        <a id="minus" href="#">Remove</a>
    </td>
</tr>
<tr>
    <td>
        <input type="text" name="expensesVO[2].strAmount" value="" id="frm_Expense_expensesVO_2__strAmount"/>
    </td>
    <td>
        <input type="text" name="expensesVO[2].memo" value="" id="frm_Expense_expensesVO_2__memo"/>
    </td>
    <td>
        <input type="submit" id="frm_Expense_ExpenseAdd_plus" name="action:ExpenseAdd_plus" value="+"/>
    </td>
</tr>
<tr>
    <td>    
        <label id="frm_Expense_transactionVO_amount">5.75</label>
    </td>
    <td align="right">
        <input type="submit" id="frm_Expense_Cancel" name="action:ExpenseAdd_cancel" value="Cancel"/>
    </td>
    <td align="left">
        <input type="submit" id="frm_Expense_Save" name="action:ExpenseAdd_save" value="Save"/>
    </td>
</tr>

回答by Chetan Sastry

$("a#minus").bind("click", function(e){
    $(this).closest('tr').hide().find('input:text').val('');
});

Note:Also see darmen's answeron why the selector a#minuswill not work as desired.

注意:另请参阅darmen 的回答,了解为什么选择器a#minus无法按预期工作。

回答by Darmen Amanbayev

You should specify a class to anchors. Binding on a single id will raise an event for the latest one.

您应该为锚点指定一个类。绑定单个 id 将引发最新的事件。

Example:

例子:

$("a.remove").live('click', function(){
   $(this).closest('tr').hide().find("input").val("");
});

回答by Anurag

Is closest defined in jQuery core or a plugin?

最接近是在 jQuery 核心还是插件中定义的?

Anyways, you could get all text boxes within that tr and then do a regex match. This should only empty the text boxes that match the id's mentioned in your question.

无论如何,您可以获得该 tr 中的所有文本框,然后进行正则表达式匹配。这应该只清空与您的问题中提到的 id 匹配的文本框。

$("a#minus").bind("click", function(e){
    var row = $(this).closest('tr');
    $("input:text", row).each(function(item) {
        var id = item.id;
        if(/frm_Expense_expensesVO_[0-9]+__strAmount/.test(id)) {
            $(item).val('');
        }
        else if(/frm_Expense_expensesVO_[0-9]+__memo/.test(id)) {
            $(item).val('');
        }
    });
    row.hide();
});

回答by geowa4

Use the classattribute, and select the two textboxes using that className. Then you can set the value of that collection to the null-string.

使用该class属性,并使用该属性选择两个文本框className。然后您可以将该集合的值设置为空字符串。