我想使用
java-script将select / drop-down数据传递给bootstrap模式.我使用模态作为确认消息.
我的选择/下拉是,
<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');
});
Bootstrap Modal如下,
<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>
我想要做的是将/ show var p_id传递给隐藏字段,并在< b id =“p_name”>< / b>一侧显示var p_name.模态.
进一步的p_id,p_name可以在用户使用上面的javascript函数从Select / Drop-down中选择任何选项时得到.我只需要做的就是如何在Modal上显示Project Name并将p_id分配到Modal的隐藏字段中
最好的祝福
最佳答案 我对这一切都很陌生,所以这可能不是最好的解决方案,但它是一个解决方案.
$('#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>
我认为这一切都是自我解释但如果没有,我们添加检查以查看项目Dropdown何时更改,然后我们使用$(this)变量分配所选项的值和文本,以声明它在#project下拉列表中ID.
接下来,我们添加一个监听器来查看模态何时显示,然后我们可以在那里操作模式我们想要的方式.在此示例中,我们设置p_name文本并设置p_id的值.