【问题标题】:Problem to concatenate AlpineJS x-text and HREF property连接 AlpineJS x-text 和 HREF 属性的问题
【发布时间】:2020-09-06 04:18:59
【问题描述】:

我有一个由 AlpineJS 提供的 Datatable 表:

<template x-for = "user in users": key = "user.Id">

在 x-for 中,我有 user.Id 的值,可以在 SPAN 字段中列出,并带有指令 x-text:

<span id = "user.Id" class = "text-gray-700 px-6 py-3 flex items-center" x-text = "user.Id"> </span>

我需要在我的 HREF 属性末尾连接 user.Id 的值,这将调用后端的路由来停用记录:

直接尝试设置HREF + user.Id属性,不行,于是想到了以下几点:

<script>
   var uid = document.getElementById ("user.Id");
   console.log ('uid value:' + uid.InnerText);
   var link = document.getElementById ("link");
   link.setAttribute ('href', 'http://api-paulinhomonteiro-com.umbler.net/cli/delete/<%= token%> /' + uid.innertText)
</script>

通过动态设置属性效果很好,但变量到达时未定义。

我该如何解决这个问题?我刚刚发现了 AlpineJS,我不能再进一步了。有人可以帮我吗?

【问题讨论】:

    标签: javascript href alpine.js


    【解决方案1】:

    要使用 Alpine 执行此操作,您需要使用 x-bind:

    <span 
      id = "user.Id"
      x-bind:href="'http://api-paulinhomonteiro-com.umbler.net/cli/delete/' + user.Id"
      class = "text-gray-700 px-6 py-3 flex items-center" 
      x-text = "user.Id"> </span>
    

    【讨论】:

    • 太棒了!!!!!!它是如此简单...@Hugo,我非常感激...我让它尽可能简单: <td class="border-dashed border-t border-gray-200 Acao"> <a x-bind:href="'&lt;a href=" http: rel="nofollow" target="_blank">api-paulinhomonteiro-com.umbler.net/cli/delete/&lt;%=token%&gt;</a>' + user.Id"&gt;Apagar </td> 你能谈谈关于 x-bind 的说法吗?我可以通过这种方法为任何元素设置任何属性吗? AlpineJS 是小型响应式应用程序的绝佳解决方案。我喜欢它。
    猜你喜欢
    • 2021-03-02
    • 1970-01-01
    • 1970-01-01
    • 2014-07-16
    • 2021-06-19
    • 1970-01-01
    • 2021-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多