jQuery选择器的设置值未定义

时间:2015-01-02 18:31:33

标签: jquery html5 twitter-bootstrap-3

我希望能够在用户的模式对话框中清除某些jQuery输入对象的值。

当用户在对话框中输入数据并使用关闭或提交按钮,然后下次用户编辑另一条记录时,输入字段的先前值仍在表单上。

调试时,jQuery选择器的值是'undefined'。

我认为这是因为我在$(document).ready函数之前使用它,但是在调试时,我确保在加载DOM之前这段代码不会执行。

如何清除输入控件的内容以供用户下次使用?

下面是我的相关HTML

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <script>
        $(document).ready(function ()
        {
            if (typeof contentPageLoadOtherDeptsTransfer == "function") contentPageLoadOtherDeptsTransfer();
        });
    </script>

<div class="modal fade" id="btnOtherDeptsTransferEditGrid" tabindex="-1" role="dialog" aria-labelledby="btnOtherDeptsTransferEditGrid-label" aria-hidden="true">
                                                <div class="modal-dialog">
                                                    <div class="modal-content">
                                                        <div class="modal-header">
                                                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                                                            <h4 class="modal-title" id="btnOtherDeptsTransferEditGrid-label">Edit Other Depts Transfer</h4>
                                                        </div>
                                                        <div class="modal-body">
                                                            <div class="modal-body">
                                                                <div class='form-group'>
                                                                    <label class="required col-lg-1 control-label" for="txtOtherDeptsTransferStatusComments">No controls in PowerPoint:</label>
                                                                    <div class="col-lg-9 col-lg-offset-1">
                                                                        <textarea id="txtOtherDeptsTransferStatusComments" rows="5" cols="80" class="form-control"></textarea>
                                                                    </div>
                                                                </div>
                                                                <div class="hide-text">
                                                                    <input type="hidden" id="txtOtherDeptsTransferEditGridID" />
                                                                </div>
                                                            </div>
                                                        </div>
                                                        <div class="modal-footer">
                                                            <button type="submit" id="btnOtherDeptsTransferAccept" class="btn btn-success">Accept</button>
                                                            <button type="button" class="btn btn-success" data-dismiss="modal">Close</button>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
</asp:Content>

这是我的相关JS:

function contentPageLoadOtherDeptsTransfer()
{

    // Clear fields in Dialog box before dialog is shown............................//
    $('#btnOtherDeptsTransferEditGrid').on('show.bs.modal', function ()
    {
        $('txtOtherDeptsTransferStatusComments').val("");
    })
}

1 个答案:

答案 0 :(得分:2)

$('txtOtherDeptsTransferStatusComments').val("");

在'txt'之前添加#

$('#txtOtherDeptsTransferStatusComments').val("");