2017-04-04 73 views
-3

我想将变量值传递给我在同一页面上调用的模态框,但尚未能做到。将变量的值传递给PHP中的模态框

代码以打开模式对话框是如下

<li style="font-style:bold" title="rate on skill" class="skill_rating"> 
<a data-toggle="modal" title="Add this item" class="open-AddBookDialog" href="#addBookDialog" data-id='<?php echo $skill_id2[$q]?>'> 
<i class="fa fa-anchor" aria-hidden="true"></i> </a><?php echo $skilltemp2[$q2] ?></li> 

模式对话框的代码如下

<div class="modal fade" id="addBookDialog" tabindex="-1" role="dialog" aria-labelledby="addBookDialog"><div class="modal-dialog" role="document"> 
     <div class="modal-content"> 
      <div class="modal-header"> 
       <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> 
       <h4 class="modal-title" id="myModalLabel">Search</h4>  
      </div> 
      <div class="modal-body"> 
       <div class="form-group"> 
       <input type="text" name="bookId" id="bookId" value=""/> 
       </div> 

      </div> 
      <div class="modal-footer"> 
       <button type="button" class="btn btn-default"><span>Search</span></button> 
      </div> 
     </div> 
    </div> 

的Javascript之前结束标记是如下

<script type="text/javascript"> 
$(document).on("click", ".open-AddBookDialog", function() { 
var myBookId = $(this).data('id'); 
$(".modal-body #bookId").val(myBookId); 
     }); 
</script> 

我在stackoverflow上看到了几个答案(如:Passing data to a bootstrap modal),但我无法按照我的要求对其进行自定义。

对不起,如果它太简单了。我是一个新手

+1

你想要的变量值传递给你的模式对话框或你想传递变量? –

+0

@GovindaRajbhar变量值 –

+1

您的JavaScript代码是错误的。您已将“点击”事件附加到文档。所以,只要你点击页面脚本就会运行。你需要在jQuery事件中学习“this”的范围。在你的情况下,“this”将代表“文档”,因此你的定制不起作用。 –

回答

2

这是在我的情况。是的,它不是在PHP中,但我已经把数据ID的静态值。

$('body').on('click', '.open-AddBookDialog', function() { 
    var myBookId = $(this).data('id'); 
    $("#addBookDialog #bookId").val(myBookId); 
}); 

JSFIDDLE

+0

不,我想要一个变量值传递给模态框 –

+0

现在,您可以在输入中设置任何变量值,只需要代表myBookId变量传递该变量即可。 –