如何在php中使用jquery在对话框中获取文本框的值,我将其作为空白或未定义

How to get value of a text box in a dialog box using jquery in php, i get it as blank or undefined

本文关键字:空白 未定义 php jquery 对话框 取文本 获取      更新时间:2023-09-26

如何在php中使用jquery获取对话框中文本框的值。我正在打开一个对话框,点击按钮,其中有一个文本框作为评论。我想把这个值提交给数据库。但是当我警告它时,我得到的值是未定义的或空白的。我无法获取文本框的消息值。我的代码如下:

<script  type="text/javascript" language="javascript">
$(document).ready(function(){
$(".QTPopup").css('display','none');
$(".lnchPopop").click(function(){
    $(".QTPopup").animate({width: 'show'}, 'slow');});
    $(".closeBtn").click(function(){            
        $(".QTPopup").css('display', 'none');
    });                   
$(document).on('click','.submit_comment',function(){
    alert("Hello");
    var comments=$('#comment_by_user').val();
    alert(comments);
    var comments1= document.getElementById("comment_by_user").value;
    alert(comments1); 
    });
});
</script>

单击注释超链接,对话框将打开。

a href="#" class="lnchPopop">Comments</a>
<div class="QTPopup" style="display: none">
<div class="QTPopupCntnr">                                                                                                             
<div class="gpBdrLeftTop"></div>                                                                                                
<div class="gpBdrRightTop"></div>                                                                                                
<div class="gpBdrTop"></div>                                                                                              
<div class="gpBdrLeft">                                                                                                       
<div class="gpBdrRight">                                                                                                             
<div class="caption">                                                                                                          
Send Your Messages                                                                                                       
</div>                                                                                                       
<a href="#" class="closeBtn" title="Close"></a>                                                                                                            
<div class="content">                                                                                                                                                                                                                                          
<br />                                                                                                                   
<table>                                                                                                                                                                                                                                                         
<tr>                                                                                                                                
<td style="height:5px;"></td>                                                                                                                         
</tr>                                                                                                                             
<tr>                                                                                                                                    
<td>&nbsp;</td>                                                                                                                            
</tr>                                                                                                                             
<tr>                                                                                                                                      
<td>                                                                                                                                             
<textarea class="textareagradiant" id="comment_by_user" name="comment_by_user" 
style="width:428px; height:116px; border:1px solid #CFCECE;"> </textarea>                                                                                                                                      
</td>                                                                                                                          
</tr>                                                                                                                            
<tr>                                                                                                                                      
<td style="height:5px;"></td>                                                                                                                           
</tr>                                                                                                                            
<tr>                                                                                                                                 
<td style="height:10px;"></td>                                                                                                                          
</tr>                                                                                                                         
<tr>                                                                                                                                    
<td>                                                                                                                                    
<input type="button" value="Submit" class="gbtn_s submit_comment"  />                                                                                                                                     
<input type="button" value="Reset" class="gbtn_s"  />                                                                                                                                     
</td>                                                                                                                             
</tr>                                                                                                                    
</table>                                                                                                              
</div>                                                                                                   
</div>                                                                                            
</div>                                                                                         
<div class="gpBdrLeftBottom"></div>                                                                                            
<div class="gpBdrRightBottom"></div>                                                                                        
<div class="gpBdrBottom"></div
</div>
</div>

删除警告("Hello");

$(document).ready(function(){
$(".QTPopup").css('display','none');
$(".lnchPopop").click(function(){
    $(".QTPopup").animate({width: 'show'}, 'slow');});
    $(".closeBtn").click(function(){            
        $(".QTPopup").css('display', 'none');
    });                   
$(document).on('click','.submit_comment',function(){
    var comments=$('#comment_by_user').val();
    alert(comments);
    var comments1= document.getElementById("comment_by_user").value;
    alert(comments1); 
    });
});

您的代码似乎工作良好。确保你修改了第一行

<a href="#" class="lnchPopop">Comments</a>

工作副本可以在这里找到:http://jsfiddle.net/6LXSn/1/.

我通常使用jQuery AJAX调用来获取php的值。
$.ajax({
    type: "POST",
    url: 'your php script',
    data: {
                comments: comments
    },
    dataType: 'html',
    async: false,
    error: function(obj, text, error) {
    },
    success: function(response) {
    }
});

然后在PHP中检索你的注释值

$_POST['comments']

希望能有所帮助