【发布时间】:2023-03-24 01:20:02
【问题描述】:
我有一个带有以下select的表格
<div class="styled-select styled-input">
<select name="upcp-sort-by" id="upcp-sort-by" onchange="UPCP_Sort_By();">
<option value="price_asc">Price (Ascending)</option>
<option value="price_desc">Price (Descending)</option>
</select>
</div>
我想用 jquery/JavaScript 将选择的文本替换为另一个文本。我从这个question 试过这个:
$(".styled-select").text(function () {
return $(this).text().replace("Price (Ascending)", "Vol (Ascending)");
});
但它破坏了选择选项..
$(".styled-select").text(function() {
return $(this).text().replace("Price (Ascending)", "Vol (Ascending)");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="styled-select styled-input">
<select name="upcp-sort-by" id="upcp-sort-by" onchange="UPCP_Sort_By();">
<option value="price_asc">Price (Ascending)</option>
<option value="price_desc">Price (Descending)</option>
</select>
</div>
我怎样才能正确地只替换选择的文本?
【问题讨论】:
-
如果您看到我的问题,我正在使用您建议的问题的答案,但它对我不起作用!
-
不同之处在于您尝试更改文本的元素是
$(".styled-select"),即<div>包围<select>。您需要更改的不是 div,甚至不是 select,而是 select 中的一个选项。 -
@PeterB 感谢您的信息!您注意到我发布的解决方案有什么问题!
标签: javascript jquery