如何用Jquery更改选项值? [英] How to change option value with Jquery?
本文介绍了如何用Jquery更改选项值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找一种方法来在用户点击链接时更改选择标记中的选项值。
I am looking for a way to change the option value from a select tag when users click on a link.
例如我有一个选择选项html:
For example I have a select option html:
<select> <option value="0">Please Select</option>
<option value="1">red</option>
<option value="2">white</option>
</select>
我有2个链接< a href =#title = redclass =preview_link> red< / a> < a href =#title =white> white< / a>
当用户点击红色时,该选项将切换为红色,白色将切换为白色。我使用以下代码,但它无法正常工作。
And I have 2 links <a href="#" title="red" class="preview_link">red</a> <a href="#" title="white">white</a>
When user clicks red the option will switch to red and white will switch to white. I am using the following code but it is not working.
jQuery("a.preview_link").click(function() {
var title = jQuery(this).attr("title");
jQuery(this).parent('p').children("select :selected").text(title);
});
有什么建议吗?
推荐答案
您的方法是将选项的文本设置为title属性。尝试:
Your way will set the option's text to the title attribute. Try:
jQuery("a.preview_link").click(function() {
var title = jQuery(this).attr("title");
jQuery(this).parent('p').find("select option").filter(function() {
return $(this).text() == title;
}).attr('selected', 'selected');
});
参见 过滤器
。
这篇关于如何用Jquery更改选项值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文