简体   繁体   中英

Empty textbox using Jquery

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

$("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

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

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

Note: Also see darmen's answer on why the selector a#minus will not work as desired.

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

Example:

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

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

Is closest defined in jQuery core or a plugin?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM