将所选值使用下划线分割。

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

Split selected value with _

问题

我正在使用Material CSS设计我的网页。我有一个单选国家列表下拉框和其值:

<select id="select_id" name="select_id" required>
  <option value="8">select1_1</option>
  <option value="15">select2_2</option>
</select>
$(document).ready(function() {
  $('select').formSelect();
});

我的选择选项来自一个带有下划线的数据库值。我想在客户端端运行时更新选择选项的值并移除下划线:

<select id="select_id" name="select_id" required>
  <option value="8">select1</option>
  <option value="15">select2</option>
</select>

是否有更好的使用jQuery来更新它的方法?感谢帮助。

英文:

I am using material css for my web page design. I am having one single select country list dropdown and the values

&lt;select id=&quot;select_id&quot; name=&quot;select_id&quot; required&gt;
  &lt;option value=&quot;8&quot;&gt;select1_1&lt;/option&gt;
  &lt;option value=&quot;15&quot;&gt;select2_2&lt;/option&gt;
&lt;/select&gt;

<!-- language: lang-js -->

 $(document).ready(function() {
   $(&#39;select&#39;).formSelect();
 });

My select option are populating from a database which has an underscore in its value. I want to update the select option value at runtime on the client side and remove the underscore from options.

&lt;select id=&quot;select_id&quot; name=&quot;select_id&quot; required&gt;
  &lt;option value=&quot;8&quot;&gt;select1&lt;/option&gt;
  &lt;option value=&quot;15&quot;&gt;select2&lt;/option&gt;
&lt;/select&gt;

Is there is any better way to update it using jQuery. Thanks for the help

答案1

得分: 0

保留原始值作为 &lt;option&gt; 标签的 "value" 属性。对于选项文本,最好的处理方式是创建一个解析器 parseOptionText(optionText: string): string,这样您完全掌握解析逻辑,代码也容易维护。

最终结果应该是:

&lt;option value={originalValueFromDB}&gt;parseOptionText(originalValueFromDB)&lt;/option&gt;

以及

const parseOptionText = (optionText: string): string =&gt; {
    return optionText.replace(&#39;_&#39;, &#39;&#39;);
}
英文:

You should keep the original value as "value" prop of the &lt;option&gt; tag. For the option text the best way to handle it is to create a parser parseOptionText(optionText: string): string, in this way you have full control over the logic of the parsing and the code is easy to maintain.

As final result you should have:

&lt;option value={originalValueFromDB}&gt;parseOptionText(originalValueFromDB)&lt;/option&gt;

And

const parseOptionText = (optionText: string): string =&gt; {
    return optionText.replace(&#39;_&#39;, &#39;&#39;);
}

答案2

得分: 0

在更改源代码是更好的解决方案的情况下,您可以使用.text()重载来更新选项的text,该重载接受一个函数

$("select option").text((i, txt) => txt.replace(/_.*$/, ''))

上述代码将从选项的文本中移除下划线。

要仅删除下划线,可以使用以下代码:

$("select option").text((i, txt) => txt.replace(/_/g, ''))
英文:

While changing at the source is the better solution, you can update the text of an option using the .text() overload that accepts a function:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(&quot;select option&quot;).text((i, txt) =&gt; txt.replace(/_.*$/, &#39;&#39;))

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;select id=&quot;select_id&quot; name=&quot;select_id&quot; required&gt;
  &lt;option value=&quot;8&quot;&gt;select1_1&lt;/option&gt;
  &lt;option value=&quot;15&quot;&gt;select2_2&lt;/option&gt;
&lt;/select&gt;

<!-- end snippet -->


> remove the underscore from options

the above matches your sample that removes both the underscore and what follows it, to remove just the underscore:

$(&quot;select option&quot;).text((i, txt) =&gt; txt.replace(/_/g, &#39;&#39;))

huangapple
  • 本文由 发表于 2023年2月10日 15:46:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75408214.html
匿名

发表评论

匿名网友

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

确定