【问题标题】:How can I append an id to href?如何将 id 附加到 href?
【发布时间】:2016-08-26 22:48:03
【问题描述】:

我不知道我的问题的正确术语是什么,但我想将客户 ID 放在 href 中,例如 href='index.php/site/get_all_services_ajax/{obj.customer[i ].customer_id}

请参阅我的代码中的第 5 行。我该怎么做?

<div id='mycoord'></div>
<script>
    $(document).ready(function() {
        $.get('<?php echo base_url();?>index.php/site/get_all_customer_ajax', function (data) {
            var obj = JSON.parse(data)
            for (var i=0; i < obj.customer.length; i++)
            {
                $('#mycoord').append("<a href='<?php echo base_url();?>index.php/site/get_customer_info/{obj.customer[i].customer_id}' <h2>" + obj.customer[i].first_name + "</href></h2><br>");
            }
        })
     });
</script>

【问题讨论】:

  • 不要用 PHP 构建 JS,更不要用 PHP 构建 JS 来构建 HTML。使东西工作所需的转义量变得更加复杂。
  • 为什么?我只是做一个简单的查看..

标签: javascript jquery json codeigniter-3


【解决方案1】:

这应该可以解决问题 -

 $(document).ready(function() { 
        $.get('<?php echo base_url();?>index.php/site/get_all_customer_ajax', function (data) {
            var obj = JSON.parse(data)
            for (var i=0;i <obj.customer.length;i++)
            {
            $('#mycoord').append("<a href='<?php echo base_url();?>index.php/site/get_customer_info/{"+obj.customer[i].customer_id+"}' <h2>" + obj.customer[i].first_name + "</href></h2><br>");
            }

        })
     });

【讨论】:

  • 谢谢它有效。 10000000000000000000 分给你。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-27
  • 2015-11-29
  • 1970-01-01
  • 2010-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多