简体   繁体   English

jQuery从模态文本区域获取旧值

[英]jquery gets the old value from the modal textarea

I have a jquery modal that is displayed for getting user's comment when the user clicks on the reject button in the page. 我有一个jquery模态,当用户单击页面中的拒绝按钮时显示该模版以获得用户的评论。

The problem is when I type some comment and confirm the reject button in the modal, I cant get the value of the commenting textarea. 问题是当我键入一些注释并确认模态中的拒绝按钮时,我无法获取注释文本区域的值。 when I reload the page, and do this process again, I get the value of the textarea before I had reloaded the page. 当我重新加载页面并再次执行此过程时,在重新加载页面之前,我获得了textarea的值。

Here is the jquery functions: 这是jquery函数:

$("#rejectBtnHOD").on('click', function(){
    $("#modal-container-522009").modal(); // Displays modal well
});

$(document).on("click", "#confirmRejection", function(){
    var vid = "<?php echo $fields['vid']; ?>"; // getting the request id well
    var rejComment = $('body').find('#comment').val(); // cant get the value of textarea

    alert(rejComment); return false;
});

HTML code (modal): HTML代码(模式):

<div class="modal fade" id="modal-container-522009" role="dialog" aria-labelledby="myModalLabel" 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">X</button>
                <h4 class="modal-title" id="myModalLabel">
                    Leave a comment!
                </h4>
            </div>
            <div class="modal-body" style="height: 300px;">
                <textarea id="comment" class="col-md-12" style="height: 200px;" ></textarea>
            </div>
            <div class="modal-footer">
                 <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> <button id="confirmRejection" type="button" class="btn btn-primary">Save changes</button>
            </div>
        </div>
    </div>
</div>

BTW, If I replace the <textarea> tag with an <input type="text"> , it works just fine! 顺便说一句,如果我将<textarea>标记替换为<input type="text"> ,它就可以正常工作!

To get the value of a text area you have to do 要获取文本区域的值,您必须做

$("#ID").html()

since you're getting the value between the XML labels. 因为您在XML标签之间获得了价值。

尝试:

var rejComment = $('#comment').val();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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