繁体   English   中英

Bootstrap数据ID未传递值

[英]Bootstrap data-id not passing value

在我的代码中

<script>
    $(document).on('click', 'a#coin', function(){
    // get month
    var val = $(this).attr('data-id');

    $.post('alert.php', {coin: val}, function(data){
        console.log(data);
    });
});
</script>   

这是模态的数据切换链接

<a id="coin" href="#" data-id="BTC" data-toggle="modal" data-target="#alertmodal"><i class="fa fa-bell fa-lg" title="Set Alert for BTC" style="color:orangered"></i></a>

在alert.php中,我只是

$coin = $_POST['coin'];
echo $coin;

模态弹出很好它只是不通过data-id值不确定我在做什么错

添加了更多代码

<?php
require('alert.php');
?>
   <html><head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="css.css" rel="stylesheet" type="text/css">
    <script src="https://code.jquery.com/jquery-3.2.1.js"></script>
    <script src="http://netdna.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
    <link href="http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">
    <script>
    $(document).ready(function(){
        $("#liveprices").load("liveprices.php");
        setInterval(function() {
            $("#liveprices").load("liveprices.php");
        }, 5000);
    });
   </script>

  </head><body>

此处讨论的功能在文档末尾加载

说bootstrap.min.js需要Jquery,尽管jquery在它的上方加载

默认情况下,引导程序使用jQuery slim版本。 Slim版本没有$.ajax和相关功能。 使用jQuery完整版。

使用这个jQuery

<script
  src="https://code.jquery.com/jquery-3.2.1.min.js"
  integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
  crossorigin="anonymous"></script>

以下是精简版的注释,其中删除了功能

/*! jQuery v3.2.1 -ajax,-ajax/jsonp,-ajax/load,-ajax/parseXML,-ajax/script,-ajax/var/location,-ajax/var/nonce,-ajax/var/rquery,-ajax/xhr,-manipulation/_evalUrl,-event/ajax,-effects,-effects/Tween,-effects/animatedSelector | (c) JS Foundation and other contributors | jquery.org/license */

讨论了有关需求的聊天之后,我们得出结论,在这种情况下完全不需要PHP,而简单的jQuery就能解决问题:

要求

如果查看此屏幕截图,您将看到带有警报图标的硬币名称列表。单击相关警报图标时,我希望它弹出一个模态,然后分配并回显一个变量值(将是硬币)。 BTC等

稍后将添加一个表单,理想情况下该表单会将数据添加到准备提交的隐藏字段中

解决方法(一种方法):

JS菲德尔

相关代码更改:

$(document).on('click', 'a.coin', function(){
    var val = $(this).attr('data-id'), modal = $('div#test-modal');
    console.log(val);
    modal.find('.modal-title').html('Get alerts for ' + val);

    modal.find('.modal-body .alert-notification').html('Get alert notifications when ' + val + ' breaks strong support / resistance lines');

    modal.modal('show');
});

由于您正在使用document.on('click') ,因此$(this)可能会有些混乱。 我可以尝试用$(event.target).closest('a#coin')代替$(this) ,看看是否有帮助:

$(document).on('click', 'a#coin', function(event) {
    var val = $(event.target).closest('a#coin');
    console.log(val);
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM