【问题标题】:How to send parameter in modal bootsrap?如何在模态引导程序中发送参数?
【发布时间】:2018-10-09 20:54:28
【问题描述】:

演示和完整代码如下:https://jsfiddle.net/xzxrp7nn/9/

我的 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>

我的 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);
        })
    });

当点击“点击我”按钮时,会显示“测试获取参数json数组”按钮。

单击“测试获取参数json数组”按钮时,我想将参数priceModal发送到模态。参数priceModal 包含 json 数组。我做 console.log(param);在$('#priceModal').on('show.bs.modal', function(e) {.

但是结果:priceModal={。 获取priceModal的值失败

有什么办法可以解决我的问题吗?

谢谢

【问题讨论】:

  • 这是什么id="priceModal='+priceModal+'"?你的意思是像这样将你的json添加到你元素的id中吗? data-price-modal="+priceModal+'" 会更合适
  • 同意@DelightedD0D。那不是id
  • @mosestoh 你想在模型中获得priceModal 值吗??

标签: javascript jquery html json bootstrap-modal


【解决方案1】:

当您调用 id="priceModal='+priceModal+'" 时,您错误地将 json 附加到元素的 id。

数据属性会更合适,更像这样:

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);
})

working jsFiddle


或者,您可以让您的服务器返回经过编码的 JSON 字符串以在属性中使用。例如,如果您使用 php,这将起作用:

echo htmlspecialchars(json_encode(someArray), ENT_QUOTES, 'UTF-8'));

【讨论】:

  • console.log(param); 的结果:{
  • @mosestoh 我的错,我忘记了 JSON 在这种情况下有点痛苦。看我的编辑,用 jQuery 添加对象并让它处理细节实际上更容易
  • 看来我会用这种方式:jsfiddle.net/o5qn5gum/2。但是存在错误
  • @mosestoh jsFiddle 没有使用上面的代码
  • 看看新的小提琴:jsfiddle.net/o5qn5gum/3。我添加console.log(JSON.parse(json));。存在错误:SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 1 column 96 of the JSON data.
【解决方案2】:

将您的JSON 放入HTML5 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="priceModel" data-json='+priceModal+'">Test get parameter json array</button>';

点击新创建的按钮访问JSON数据。

var json = $("#priceModel").attr("data-json");
$(".modal-body").html(json); //For eg.

JSFiddle

【讨论】:

  • 我不认为这会很有效,看到这个jsFiddle,我很确定你需要对字符串进行编码,以便以后能够从中获取任何有用的东西,如果你将它设置在html 本身
  • @DelightedD0D 你检查给定的小提琴 URL 了吗??
  • @DelightedD0D,看来我会用这种方式。但是当我尝试 jsfiddle 时,存在错误。错误:SyntaxError: JSON.parse: expected property name or '}' at line 1 column 2 of the JSON data。好像转换成对象失败了
  • 没错,我就是这么说的,如果你想这样做,你必须在服务器上对字符串进行编码
  • 已解决。 var isitable = '&lt;button class="btn blue tes_button" data-toggle="modal" data-target="#priceModal" id="priceModel" data-json='+priceModal+'"&gt;Test get parameter json array&lt;/button&gt;'; 中的多余双引号。非常感谢
猜你喜欢
  • 2015-04-22
  • 1970-01-01
  • 1970-01-01
  • 2014-08-11
  • 2021-09-08
  • 2018-10-03
  • 2015-07-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多