如何用Jquery更改选项值?

我正在寻找一种方法来在用户点击链接时从选择标记更改选项值。

例如,我有一个选择选项html:

 Please Select red white  

我有2个链接red white当用户点击红色时该选项将切换为红色,白色将切换为白色。 我使用以下代码但它无法正常工作。

  jQuery("a.preview_link").click(function() { var title = jQuery(this).attr("title"); jQuery(this).parent('p').children("select :selected").text(title); }); 

有什么建议?

您的方法将选项的文本设置为title属性。 尝试:

  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'); }); 

见过filter

这个?

 $("select #some-option").attr("selected", "selected"); 

或这个?

 $("select").val(index); 

嗯,你错了:不需要更改所选选项的文本,而是需要遍历元素并选择合适的元素,如下所示:

 $("select option").each(function() { if($(this).text() == "red")) { this.selected = true; } }); 

编辑:jQuery中可能有一个select()函数。 如果是这种情况,那么在DOM属性上使用它。