引导模式文本区域在单击时显示空警报 [英] bootstrap modal textarea showing empty alert on click jquery

查看:84
本文介绍了引导模式文本区域在单击时显示空警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在模态弹出式窗口中循环显示数据库值.在弹出窗口中有一个回复textarea,我想使用ajax发布该值.但是当我使用jquery的click事件提醒值时,我得到了textarea的空值.这是我的代码

i have modal popup in loop with database values. In popup there is a reply textarea and i want to post that value using ajax. But i am getting empty value of textarea when i alert value on click event using jquery. Here is my code

代码:

<?php 
$sn=1;$rep=1;
foreach($view_queries as $q) {?>
<a href="#" data-toggle="modal" data-target="#create_reply'.$rep.'" style="color:red;">Reply</a>
?>
<!-- create reply model -->
<div id="create_reply<?php echo $rep;?>" class="modal fade" role="dialog">
<div class="modal-dialog">

<!-- Modal content-->
<div class="modal-content">
<form id="send_reply" method="post" antype="multipart/form-data">
<input type="hidden" name="query_id" value="<?php echo $q->query_id;?>" />
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">&times;</button>
<h4 class="modal-title">Reply To Query</h4>
</div>
<div class="modal-body">
<label>Query:</label>
<p><?php echo ucfirst($q->query);?></p>

<div class="form-group">
<label>Reply:</label>
<textarea rows="3" name="mesg" id="<?php echo $sn;?>mesg" class="form-control"></textarea>
</div>

</div>
<div class="modal-footer">
<button type="button" id="" data-target="<?php echo $sn;?>mesg" class="btn btn-default form_click">Reply</button>
</div>
</form>
</div>

</div>
</div>
<!-- create reply model ends -->
<?php $sn++; $rep++; }?>

这里是提醒您检查价值的脚本:

HERE IS THE SCRIPT FOR CHECKING VALUE IN ALERT:

 <script>
 $(document).ready(function(){
  $(document).on("click",".form_click",function(event){
  event.preventDefault();
  var a=$('#' + $(this).data('target')).text();
  alert(a);
  });
 });
 </script>

推荐答案

您应该使用.val()而不是.text(). text()获取HTML元素的HTML内容. val()用于获取HTML输入的内容.

You should use .val() instead of .text(). text() get's the HTML contents of a HTML element. val() is used for getting contents of HTML inputs.

正如史密斯·拉瓦尔(Smit Raval)所述,id应该是唯一的.

Also as Smit Raval already stated, id's should be unique.

您可以为字段提供一个ID,并在PHP中设置索引. 因此,在每个循环中,您都增加索引并将其添加到您的id值中,如下所示: id = "<?=$index?>_mesg".

You could give the fields an id with and index set in PHP. So in every loop you increment the index and add that to your id values, like so: id="<?=$index?>_mesg".

然后在您的按钮中执行: <button type="button" data-target="<?=$index?>_mesg" id="" class="btn btn-default form_click">Reply</button>

And in your button do: <button type="button" data-target="<?=$index?>_mesg" id="" class="btn btn-default form_click">Reply</button>

然后在您的jquery中,您可以执行var a = $('#'+ $(this).data('target')).val();

And then in your jquery you can do var a=$('#' + $(this).data('target')).val();

在按钮中查看添加的数据目标. jQuery中的$(this).data('target')用于检索目标数据属性的值.

See the added data-target in your button. And the $(this).data('target') in the jquery for retrieving the value of the target data attribute.

这篇关于引导模式文本区域在单击时显示空警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆