英文:
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
<select id="select_id" name="select_id" required>
<option value="8">select1_1</option>
<option value="15">select2_2</option>
</select>
<!-- language: lang-js -->
$(document).ready(function() {
$('select').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.
<select id="select_id" name="select_id" required>
<option value="8">select1</option>
<option value="15">select2</option>
</select>
Is there is any better way to update it using jQuery. Thanks for the help
答案1
得分: 0
保留原始值作为 <option>
标签的 "value" 属性。对于选项文本,最好的处理方式是创建一个解析器 parseOptionText(optionText: string): string
,这样您完全掌握解析逻辑,代码也容易维护。
最终结果应该是:
<option value={originalValueFromDB}>parseOptionText(originalValueFromDB)</option>
以及
const parseOptionText = (optionText: string): string => {
return optionText.replace('_', '');
}
英文:
You should keep the original value as "value" prop of the <option>
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:
<option value={originalValueFromDB}>parseOptionText(originalValueFromDB)</option>
And
const parseOptionText = (optionText: string): string => {
return optionText.replace('_', '');
}
答案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 -->
$("select option").text((i, txt) => txt.replace(/_.*$/, ''))
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="select_id" name="select_id" required>
<option value="8">select1_1</option>
<option value="15">select2_2</option>
</select>
<!-- 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:
$("select option").text((i, txt) => txt.replace(/_/g, ''))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论