对于循环生成URL不起作用

时间:2021-03-28 18:06:00

I want to write {{ url('character/c1') }} to be localhost:8000/character/c1 but it didn't work inside JavaScript function.

我想写{{url('character / c1')}}为localhost:8000 / character / c1但它在JavaScript函数中不起作用。

It resulting the exact localhost:8000/{{ url('character/c1') }} link instead.

它产生了确切的localhost:8000 / {{url('character / c1')}}链接。

Here's my code:

这是我的代码:

<script type="text/javascript">

    var characters = [
      "geer",
      "daar",
      "geet",
      "geen"
    ]

    var character = "";
    var i;

    for (i = 0; i < characters.length; i++) {
      character += "<a href=\"\{\{ url('character/c" + i + "') \}\}\"><li></li>" + "</a>";
    }

    document.getElementById('characters').innerHTML = character;
</script>

<ul id="characters"></ul>

3 个解决方案

#1


1  

Write your for loop as this,

写下你的for循环,

var url = {{ url('character') }};
for (i = 0; i < characters.length; i++) {
  character += '<a href="' + url + '/c' + i + '"><li></li></a>';
}

#2


1  

Add this line before the for loop

在for循环之前添加此行

var url = window.location; // To get current window url
var url = "<?php echo $_SERVER['SERVER_NAME'] ?>" + ":<?php echo $_SERVER['SERVER_PORT'] ?>"; // To get your server name with Port

Then inside the for loop modify the line like this.

然后在for循环中修改这样的行。

character += "<a href=\"\{\{" +  url + "('character/c" + i + "') \}\}\"><li><img src=\"character/list/c" + i +  "/icon.png\"></li>" + "</a>";

window.location will give you current url. Ofcourse, if you want your server name only. You can take the php option.

window.location将为您提供当前网址。当然,如果你只想要你的服务器名称。你可以选择php选项。

Cheers. Happy coding.

干杯。快乐的编码。

#3


0  

Thank you everyone, I have figure it out myself. It's because c1 is inside a route get(/character/{id})

谢谢大家,我已经弄清楚了。这是因为c1位于路径get(/ character / {id})内

for (i = 0; i < characters.length; i++) {
  character += '<a href="c' + i + '"><li></li></a>';
}

#1


1  

Write your for loop as this,

写下你的for循环,

var url = {{ url('character') }};
for (i = 0; i < characters.length; i++) {
  character += '<a href="' + url + '/c' + i + '"><li></li></a>';
}

#2


1  

Add this line before the for loop

在for循环之前添加此行

var url = window.location; // To get current window url
var url = "<?php echo $_SERVER['SERVER_NAME'] ?>" + ":<?php echo $_SERVER['SERVER_PORT'] ?>"; // To get your server name with Port

Then inside the for loop modify the line like this.

然后在for循环中修改这样的行。

character += "<a href=\"\{\{" +  url + "('character/c" + i + "') \}\}\"><li><img src=\"character/list/c" + i +  "/icon.png\"></li>" + "</a>";

window.location will give you current url. Ofcourse, if you want your server name only. You can take the php option.

window.location将为您提供当前网址。当然,如果你只想要你的服务器名称。你可以选择php选项。

Cheers. Happy coding.

干杯。快乐的编码。

#3


0  

Thank you everyone, I have figure it out myself. It's because c1 is inside a route get(/character/{id})

谢谢大家,我已经弄清楚了。这是因为c1位于路径get(/ character / {id})内

for (i = 0; i < characters.length; i++) {
  character += '<a href="c' + i + '"><li></li></a>';
}