如何在不删除值的情况下在选项文本上添加文本?
How can i add text on option text without remove values?
我在尝试添加一些文本而不更改选项选择值时遇到问题。
你能帮我解决这个问题吗?
我有这个HTML代码
<select id="q_c_0_a_0_name">
<option value="name"> Name</option>
<option value="lastanem1">First Lastname</option>
<option value="lastname2">Second Lastname</option>
...
<option value="xxxxxxxxx">Other value</option>
</select>
我正在尝试在不删除值的情况下向文本选项添加文本。
<select id="q_c_0_a_0_name">
<option value="name"> Filter by Name</option>
<option value="lastanem1">Filter by First Lastname</option>
<option value="lastname2">Filter by Second Lastname</option>
...
<option value="xxxxxxxxx">Filter by Other value</option>
</select>
我试过这个 Jquery 代码:
$(function(){
$("#q_c_0_a_0_name option[value=cont]").textContent += "Filter by";
});
也尝试了这个 Jquery 代码:
$(function(){
$("#q_c_0_a_0_name option[value]").textContent += "Filter by";
});
使用text()
$("#q_c_0_a_0_name option").text(function() {
return "Filter by " + $(this).text()
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="q_c_0_a_0_name">
<option value="name"> Name</option>
<option value="lastanem1">First Lastname</option>
<option value="lastname2">Second Lastname</option>
<option value="xxxxxxxxx">Other value</option>
</select>
<select id="q_c_0_a_0_name">
<option value="name">Name</option>
<option value="lastanem1">First Lastname</option>
<option value="lastname2">Second Lastname</option>
<option value="xxxxxxxxx">Other value</option>
</select>
<script>
$('#q_c_0_a_0_name').find($('option').prepend('Filter By '));
</script>
我在尝试添加一些文本而不更改选项选择值时遇到问题。
你能帮我解决这个问题吗?
我有这个HTML代码
<select id="q_c_0_a_0_name">
<option value="name"> Name</option>
<option value="lastanem1">First Lastname</option>
<option value="lastname2">Second Lastname</option>
...
<option value="xxxxxxxxx">Other value</option>
</select>
我正在尝试在不删除值的情况下向文本选项添加文本。
<select id="q_c_0_a_0_name">
<option value="name"> Filter by Name</option>
<option value="lastanem1">Filter by First Lastname</option>
<option value="lastname2">Filter by Second Lastname</option>
...
<option value="xxxxxxxxx">Filter by Other value</option>
</select>
我试过这个 Jquery 代码:
$(function(){
$("#q_c_0_a_0_name option[value=cont]").textContent += "Filter by";
});
也尝试了这个 Jquery 代码:
$(function(){
$("#q_c_0_a_0_name option[value]").textContent += "Filter by";
});
使用text()
$("#q_c_0_a_0_name option").text(function() {
return "Filter by " + $(this).text()
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="q_c_0_a_0_name">
<option value="name"> Name</option>
<option value="lastanem1">First Lastname</option>
<option value="lastname2">Second Lastname</option>
<option value="xxxxxxxxx">Other value</option>
</select>
<select id="q_c_0_a_0_name">
<option value="name">Name</option>
<option value="lastanem1">First Lastname</option>
<option value="lastname2">Second Lastname</option>
<option value="xxxxxxxxx">Other value</option>
</select>
<script>
$('#q_c_0_a_0_name').find($('option').prepend('Filter By '));
</script>