如何使用jq在p标签中设置值?

时间:2016-01-16 06:28:24

标签: javascript jquery html dom

我正在尝试搜索银行名称onkeyup并希望将结果放入p标签,但它没有发生。可以帮助我吗?

这是jq://搜索银行名称

$('input[name^="acc_voucher_bank[]"]').live('keyup',function(){     
    var acc_voucher_bank=$(this).val();
    $(this).closest('tr').find('p.bankNameResult').show();        
    $.post("../accounting_model/cash_receive_daily_date_wise.php",{   
        acc_voucher_bank:acc_voucher_bank,         
        rand:Math.random()
        },function(data){   
        $(this).closest('tr').find('p.bankNameResult').html(data);
    })
})

html代码:

<table width="100%" id="myTable">
    <tr class="tr_view">
        <td style="width:25%;">Bank Name</td>
        <td style="width:30%;">Branch Name</td>
        <td style="width:15%;">A/C. No</td>
        <td style="width:15%;">Amount</td>
        <td style="width:15%;">Action</td>
    </tr>

    <tr class="bg1" style="text-align:center;">
        <td>
            <input type="text" style="width:200px;" name="acc_voucher_bank[]" id="" placeholder="Bank Name" />
            <p id="bankNameResult" class="bankNameResult" name="bankNameResult[]" style="position:absolute; margin:0px;"></p>
            <input type="hidden" name="bank_id[]" id="" />
        </td>

        <td>
            <input type="text" style="width:270px;" name="acc_voucher_branch[]" id="" placeholder="Branch Name" />
            <p id="bankBranchNameResult" class="bankBranchNameResult" name="bankBranchNameResult[]" style="position:absolute; margin:0px;"></p>
            <input type="hidden" name="bank_branch_id[]" id="" />
        </td>

        <td>
            <input type="text" style="width:130px;" name="acc_bank_account_number[]" id="" placeholder="A/C No" />
            <p id="bankAccountResult" class="bankAccountResult" name="bankAccountResult[]" style="position:absolute; margin:0px;"></p>
            <input type="hidden" name="bank_account_no[]" id="" />
        </td>

        <td><input type="text" style="width:130px;" name='bank_amount[]' id="" placeholder="Amount" /></td>

        <td><input type="button" value="+" class="addTr" /></td>
    </tr>
</table>

1 个答案:

答案 0 :(得分:1)

那是因为this回调函数中的$.post是一个jQuery XHR对象。您可以将要更改的元素分配给另一个变量,以便在$.post回调中不会更改:

$('input[name^="acc_voucher_bank[]"]').live('keyup',function(){     
    var acc_voucher_bank=$(this).val();
    $element = $(this).closest('tr').find('p.bankNameResult');
    $element.show();    
    $.post("../accounting_model/cash_receive_daily_date_wise.php",{   
        acc_voucher_bank:acc_voucher_bank,         
        rand:Math.random()
        },function(data){   
        $element.html(data);
    })
})
相关问题