使用jquery按值选择DropDown [英] Select a DropDown by values with jquery

查看:60
本文介绍了使用jquery按值选择DropDown的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<select id="id_deals-1-deal_template" name="deals-1-deal_template">
 <option selected="selected" value="">---------</option>
 <option value="1" selected="selected">Pear</option>
 <option value="2">Apple</option>
 <option value="4">Melon</option>
</select>

为了选择甜瓜,我必须这样做:

In order to select Melon I have to do this:

$('#id_deals-1-deal_template>option:eq(3)').prop('selected', true);

但我宁愿在这种情况下选择4的主键。
因此可以按其值而不是序列选择下拉列表吗?

But I would rather have to select the primary key that is 4 in this case. Hence is it possible to select a dropdown by its value rather than sequence?

推荐答案

尝试使用 .val 可以是基于参数的setter或getter。下面是一个setter ..

Try using .val which can be a setter or getter based on the argument. Below is a setter..

$('#id_deals-1-deal_template').val(4);

这篇关于使用jquery按值选择DropDown的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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