HTML 给元素名称分配变量

huangapple go评论52阅读模式
英文:

HTML assign variables to element name

问题

有没有办法将变量分配给一个元素名称,以便我可以访问该元素并更改其值:

我可以格式化 updatedt 的日期时间:

let dt_formatted = convertDateFormat("[% order.updatedt %]");
$("input[name='updatedt']").val(dt_formatted);

不幸的是,将 [% field %] 分配给 name 不会为 name 分配任何值:

<dd name=[% field %] class="float-right">[% order.$field %]</dd>
英文:

Is there a way to assign variable to an Element Name so that I can access the element and change the values:


[% FOREACH field IN [&#39;id&#39;,&#39;type&#39;,&#39;updatedt&#39;,&#39;lastcheckdt&#39;] %]
	&lt;div class=&quot;row col-md-3 col-sm-6&quot;&gt;
		&lt;dl class=&quot;details-dl&quot;&gt;
			&lt;label&gt;[% field %]&lt;/label&gt;
			&lt;div class=&quot;details-dg&quot;&gt;
				&lt;dd name=[% field %] class=&quot;float-right&quot;&gt;[% order.$field %]&lt;/dd&gt;	
			&lt;/div&gt;
		&lt;/dl&gt;
	&lt;/div&gt;
[% END %] 

I can format the datetime for updatedt:

let dt_formatted = convertDateFormat(&quot;[% order.updatedt %]&quot;);
$( &quot;[name=&#39;updatedt&#39;]&quot; ).val(dt_formatted);

Unfortunate assigning [ % field %] to name does not assign any value to name:

&lt;dd name=[% field %] class=&quot;float-right&quot;&gt;[% order.$field %]&lt;/dd&gt;	

答案1

得分: 0

看起来你这里缺少引号:

&lt;dd name=&quot;[% field %]&quot; ...&gt;
英文:

Looks like your are missing the quotes here:

&lt;dd name=&quot;[% field %]&quot; ...&gt;

答案2

得分: 0

使用.html()而不是.val()来访问<dd>中的属性。

$( "[name='updatedt']" ).val(dt_formatted); --> 错误
$( "[name='updatedt']" ).html(dt_formatted); --> 正确
英文:

use .html() instead of .val to access the property in &lt;dd&gt;

$( &quot;[name=&#39;updatedt&#39;]&quot; ).val(dt_formatted); --&gt; wrong
$( &quot;[name=&#39;updatedt&#39;]&quot; ).htmtl (dt_formatted); --&gt; correct

huangapple
  • 本文由 发表于 2023年7月17日 09:50:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76701088.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定