如何通过Jquery链接更改选项值? [英] How to change option value from a link with Jquery?

查看:63
本文介绍了如何通过Jquery链接更改选项值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种当用户单击链接时从Asp ListItem控件更改选项值的方法.

例如,我有一个Asp ListItem控件:

I am looking for a way to change the option value from a Asp ListItem control when users click on a link.

For example I have a Asp ListItem control:

<asp:DropDownList ID="DropDownList1" runat="server" class="Preview_link"

    Height="20px" Width="118px">

    <asp:ListItem>Red</asp:ListItem>
    <asp:ListItem>White</asp:ListItem>
    <asp:ListItem>Green</asp:ListItem>
    <asp:ListItem>Blue</asp:ListItem>
</asp:DropDownList>


我有2个链接


And I have 2 links

<a href="#" title="red" class="preview_link">red</a> <a href="#" title="white">white</a>


当用户单击红色时,该选项将切换为红色,而白色将切换为白色.我正在使用以下代码,但无法正常工作.

我将如何更改此jquery以使其与Asp listItem控件(而不是< select>/< option>)一起使用?


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.

How would I go about changing this jquery to work with a Asp listItem control instead of a <select>/<option>?

<pre lang="cs">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'');  });</pre>

推荐答案

(this).text()== title; }).attr("selected","selected"); });</pre>
(this).text() == title; }).attr(''selected'', ''selected''); });</pre>


您好,

我可以发现三个可能会阻止此工作的问题.一个是大写字母,另一个是大写字母-两者都可能停止您的比较.另一个没有取消选择当前选择的选项. JavaScript可能应如下所示:(我已将jQuery更改为推荐/正确使用
Hi there,

I can spot three problems that might be stopping this working. One is capitalisation and another is not trimming - both might stop your comparison. The other is not deselecting the currently selected option. The JavaScript should probably be as follows: (I have changed jQuery to the recommended/proper use of


,并将==更改为推荐使用===)

and changed == to recommended use of ===)


这篇关于如何通过Jquery链接更改选项值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆