如何更改选择选项文本

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

How to change select option text

问题

当我为select标签设置has-empty-data=true属性时,将会添加一个空选项到选项列表中。那么如何为这个空选项标签设置文本呢?

<select name="address[province]" title="{{ 'customer.addresses.province' | t }}" has-empty-data="true" data-default="{{ form.province }}" id="provinceExist"></select>

我尝试了这样做,但仍然不起作用:

document.getElementsByName('provinceExist')[0].options[0].innerHTML = "Water";

请注意,这里使用的是JavaScript代码,用于设置空选项标签的文本。

英文:

如何更改选择选项文本
When I set the has-empty-data=true attribute for the select tag, an empty option will be added to the list option. so how can I set the text for this empty option tag?

&lt;select name=&quot;address[province]&quot; title=&quot;{{ &#39;customer.addresses.province&#39; | t }}&quot; has-empty-data=&quot;true&quot; data-default=&quot;{{ form.province }}&quot; id=&quot;provinceExist&quot;&gt;&lt;/select&gt;

如何更改选择选项文本
I tried doing this but it still doesn't work

document.getElementsByName(&#39;provinceExist&#39;).options[0].innerHTML = &quot;Water&quot;;

答案1

得分: 1

你需要使用 getElementById 而不是 getElementByName,因为你的字段具有名为 provinceExistid

另外,你需要添加 ContentLoaded 事件来更改文本,或者你需要创建一个在事件发生时调用的函数。我已经以内容加载事件为例进行了演示。

window.addEventListener("DOMContentLoaded", () => {
    document.getElementById("provinceExist").options[0].text = "Water";
});

以下是示例选择框:

<select name="address[province]" id="provinceExist">
  <option>Test</option>
  <option>Test1</option>
  <option>Test2</option>
</select>
英文:

You need to used getElementById instead of getElementByName as your field have id called provinceExist:

Also, you need to add ContentLoaded event for change text or you need to create function to call on event happen. I have taken Content loaded event as an example.

    window.addEventListener(&quot;DOMContentLoaded&quot;, () =&gt; {
        document.getElementById(&quot;provinceExist&quot;).options[0].text = &quot;Water&quot;;
    });

Here example select box:

&lt;select name=&quot;address[province]&quot; id=&quot;provinceExist&quot;&gt;
  &lt;option&gt;Test&lt;/option&gt;
  &lt;option&gt;Test1&lt;/option&gt;
  &lt;option&gt;Test2&lt;/option&gt;
&lt;/select&gt;

答案2

得分: 0

你可以尝试使用Option对象的text属性:

document.getElementsByName('provinceExist').options[0] = new Option('Water', '');

你也可以使用innerHTML属性来设置空白选项标签的文本,就像这样:

document.getElementsByName('provinceExist').options[0].innerHTML = 'Water';

getElementsByName返回一个元素集合,所以你需要访问集合中的第一个元素才能设置innerHTML或text属性。

英文:

You can try using the text property of the Option object:

document.getElementsByName(&#39;provinceExist&#39;).options[0] = new Option(&#39;Water&#39;, &#39;&#39;);

You can also use the innerHTML property to set the text for the empty option tag, like this:

document.getElementsByName(&#39;provinceExist&#39;).options[0].innerHTML = &#39;Water&#39;;

getElementsByName returns a collection of elements, so you need to access the first element in the collection to be able to set the innerHTML or text property.

答案3

得分: 0

document.getElementsByName('address[province]').options[0].innerHTML = "Water";

instead of 

document.getElementsByName('provinceExist').options[0].innerHTML = "Water";

or if you want to use the id you could use 

document.getElementsById('provinceExist').options[0].innerHTML = "Water";


there's also another neatway instead of having to go through all this 

<select>
<option hidden disabled selected value> -- select an option -- </option>


  <option>Option 1</option>
  <option>Option 2</option>
  <option>Option 3</option>
</select>

this will show up '-- select an option --' and once you select something this option won't no longer exist and you won't be able to select it back/view it again
英文:
document.getElementsByName(&#39;address[province]&#39;).options[0].innerHTML = &quot;Water&quot;;

instead of

document.getElementsByName(&#39;provinceExist&#39;).options[0].innerHTML = &quot;Water&quot;;

or if you want to use the id you could use

document.getElementsById(&#39;provinceExist&#39;).options[0].innerHTML = &quot;Water&quot;;

there's also another neatway instead of having to go through all this

&lt;select&gt;
&lt;option hidden disabled selected value&gt; -- select an option -- &lt;/option&gt;


  &lt;option&gt;Option 1&lt;/option&gt;
  &lt;option&gt;Option 2&lt;/option&gt;
  &lt;option&gt;Option 3&lt;/option&gt;
&lt;/select&gt;

this will show up -- select an option -- and once you select something this option won't no longer exist and you won't be able to select it back/view it again

huangapple
  • 本文由 发表于 2023年1月9日 14:49:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75053943.html
匿名

发表评论

匿名网友

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

确定