最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Problem to concatenate AlpineJS x-text and HREF property - Stack Overflow

programmeradmin7浏览0评论

I have a Datatable table being fed by AlpineJS:

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

In x-for, I have the value of user.Id that I can list in a SPAN field, with the instruction x-text:

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

I need to concatenate the value of user.Id at the end of my HREF property, which will call a route on the backend to inactivate a record:

Directly, trying to set the HREF + user.Id property, it didn't work, so I thought about the following:

<script>
   var uid = document.getElementById ("user.Id");
   console.log ('uid value:' + uid.InnerText);
   var link = document.getElementById ("link");
   link.setAttribute ('href', '/<%= token%> /' + uid.innertText)
</script>

It worked very well by setting the property dynamically but the variable arrives as undefined.

How could I solve this? I just discovered AlpineJS and I can't go any further. Can someone help me?

I have a Datatable table being fed by AlpineJS:

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

In x-for, I have the value of user.Id that I can list in a SPAN field, with the instruction x-text:

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

I need to concatenate the value of user.Id at the end of my HREF property, which will call a route on the backend to inactivate a record:

Directly, trying to set the HREF + user.Id property, it didn't work, so I thought about the following:

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

It worked very well by setting the property dynamically but the variable arrives as undefined.

How could I solve this? I just discovered AlpineJS and I can't go any further. Can someone help me?

Share Improve this question asked May 19, 2020 at 23:02 Paulo MonteiroPaulo Monteiro 931 gold badge1 silver badge9 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 16

To do this with Alpine you need to use 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>
发布评论

评论列表(0)

  1. 暂无评论