我希望使用java-script将select/下拉数据传递到引导模式中。我正在用模态作为确认信息。
我的选择/下降是,
<select class="form-control" id="project" name="project_id">
<option value="1"> ABCD </option>
<option value="2"> EFGH </option>
<option value="3"> IJKL </option>
<option selected="selected" value="#">Please Select</option>
</select>我使用javascript调用Bootstrap Modal,如下所示,
$('#project').change(function(){
var e = document.getElementById("project");
var p_id = e.options[e.selectedIndex].value;
var p_name = e.options[e.selectedIndex].text;
$('#myModal').modal('show');
});启动模式如下,
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<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">×</span></button>
<h4 class="modal-title" id="myModalLabel"></h4>
</div>
<div class="modal-body modal-mt-hgt">
<form action="" method="post">
<p> Are you sure to comment on <b id="p_name">...PROJECT NAME HERE</b> </p>
Enter Your Comment : <textarea rows="3"></textarea>
<input type="hidden" name="country" value="">
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Add Task</button>
</div>
<?= form_close(); ?>
</div>
</div>
</div>我想要做的是将var p_id传递/显示到隐藏字段中,并在模态的<b id="p_name"></b>侧显示var p_name。
此外,当用户使用上面的javascript函数从选择/下拉菜单中选择任何选项时, p_id ,p_name可以得到,唯一需要做的就是如何在Modal上显示项目名称,并将p_id分配到Modal的隐藏字段中。
诚挚的问候
发布于 2015-12-21 08:13:06
我对所有这些都很陌生,所以这可能不是最好的解决方案,但它是一个解决方案。
$('#project').on('change', function() {
var p_id = $(this).val();
var p_name = $(this).find(':selected').text();
$('#myModal').on('show.bs.modal', function ()
{
$(this).find('#p_name').text(p_name);
$(this).find('#p_id').val(p_id);
});
$('#myModal').modal('show');
});您需要向隐藏字段中添加一个类或ID,以便对其进行标识。上面我给了它一个p_id ID。
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<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">×</span></button>
<h4 class="modal-title" id="myModalLabel"></h4>
</div>
<div class="modal-body modal-mt-hgt">
<form action="" method="post">
<p> Are you sure to comment on <b id="p_name">...PROJECT NAME HERE</b> </p>
Enter Your Comment : <textarea rows="3"></textarea>
<input type="hidden" id="p_id" name="country" value="">
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Add Task</button>
</div>
<?= form_close(); ?>
</div>
</div>
</div>我认为这都是不言自明的,但如果不是,我们添加检查以查看项目下拉列表何时发生变化,然后使用$(this)变量分配所选项的值和文本,以声明它在#project下拉ID中。
接下来,我们添加一个侦听器来查看模式何时显示,然后我们可以按我们想要的方式操作这个模式。在本例中,我们设置了p_name文本并设置了p_id的值。
https://stackoverflow.com/questions/34390884
复制相似问题