[英]How to send parameter in modal bootsrap?
Demo and full code is like this : https://jsfiddle.net/xzxrp7nn/9/演示和完整代码是这样的: https : //jsfiddle.net/xzxrp7nn/9/
My HTML code is like this :我的 HTML 代码是这样的:
<button type="button">Click Me</button>
<div id="tes"></div>
<!-- Modal Currency-->
<div class="modal fade" id="priceModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
</div>
<div class="modal-body">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
My Javascript code is like this :我的 Javascript 代码是这样的:
$(document).ready(function(){
$("button").click(function(){
$.ajax({
//type: 'POST',
//url: 'script.php',
success: function(data) {
var priceModal = '{"attributes":{"Code":"DBL","Total":"200000"},"DayPrice":{"Date":"2016-05-26","Rate":"200000"}}';
var isitable = '<button class="btn blue tes_button" data-toggle="modal" data-target="#priceModal" id="priceModal='+priceModal+'">Test get parameter json array</button>';
$("#tes").html(isitable);
}
});
});
$('#priceModal').on('show.bs.modal', function(e) {
//console.log('yes');
var param = e.relatedTarget.id;
// var hotel_code = $(this).data('id');
console.log(param);
// console.log(hotel_code);
})
});
When click button "Click me", It will display button "Test get parameter json array".单击“单击我”按钮时,将显示“测试获取参数 json 数组”按钮。
When click button "Test get parameter json array", I want send parameter priceModal
to modal.当单击按钮“测试获取参数 json 数组”时,我想将参数priceModal
发送到模态。 Parameter priceModal
contain json array.参数priceModal
包含 json 数组。 I do console.log(param);我做 console.log(param); in $('#priceModal').on('show.bs.modal', function(e) {
.在$('#priceModal').on('show.bs.modal', function(e) {
.
But the result : priceModal={
.但结果是: priceModal={
。 It failed to get the value of priceModal获取 priceModal 的值失败
Any solution to solve my problem?有什么解决方案可以解决我的问题?
Thank you谢谢
When you call id="priceModal='+priceModal+'"
you are incorrectly appending your json to your element's id. 当您调用id="priceModal='+priceModal+'"
您错误地将json附加到元素的ID中。
A data attribute would be far more appropriate, something more like this: 数据属性将更合适,类似于以下内容:
success: function(data) {
var priceModal = {"attributes":{"Code":"DBL","Total":"200000"},"DayPrice":{"Date":"2016-05-26","Rate":"200000"}};
var isitable = '<button class="btn blue tes_button" data-toggle="modal" data-target="#priceModal" data-price-modal="">Test get parameter json array</button>';
$("#tes").html(isitable).find('[data-price-modal]').data('price-modal',priceModal);
}
$('#priceModal').on('show.bs.modal', function(e) {
var param = $('[data-price-modal]').data('price-modal');
console.log(param);
})
Alternatively, you could have your server return the JSON string encoded for use in the attribute. 或者,您可以让服务器返回编码为在属性中使用的JSON字符串。 For example, if you were using php
, this would work: 例如,如果您使用的是php
,那么它将起作用:
echo htmlspecialchars(json_encode(someArray), ENT_QUOTES, 'UTF-8'));
Place your JSON
in HTML5 data-* 将您的JSON
放入HTML5数据中-*
var priceModal = "{'attributes':{'Code':'DBL','Total':'200000'},'DayPrice':{'Date':'2016-05-26','Rate':'200000'}}";
var isitable = '<button class="btn blue tes_button" data-toggle="modal" data-target="#priceModal" id="priceModel" data-json='+priceModal+'">Test get parameter json array</button>';
Access JSON
data upon clicking newly Created button. 单击新创建的按钮,访问JSON
数据。
var json = $("#priceModel").attr("data-json");
$(".modal-body").html(json); //For eg.
if you want just get json array in modal use this way: 如果您只想以模式获取json数组,请使用以下方式:
<button type="button" data-toggle="modal" data-target="#priceModal" data-whatever="value">Click Me</button>
in jquery: 在jQuery中:
$('#priceModal').on('show.bs.modal', function(e) {
//get data from trigger element fire the modal
var sample= $(this).data('whatever');
// you can send parameter to server and get data to display in modal
$.ajax({
type: 'GET',
url: 'action url',
data:{param:sample},
async:false,
success: function(data) {
console.log(data);
},
error:function(xhr){
console.log(xhr.responseText);
}
});
})
if you want use the your way change this line to get your data. 如果要使用自己的方式,请更改此行以获取数据。 html attributes start " and end " because of this you get only this value priceModal={ html属性以“开始”和“结束”开头,因此,您仅获得此值priceModal = {
var priceModal = "{'attributes':{'Code':'DBL','Total':'200000'},'DayPrice':{'Date':'2016-05-26','Rate':'200000'}}"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.