获取上一个选择的下拉项
我有以下代码,其中有一个下拉列表(带有 class="addToList" ,后跟一个按钮(Class="addtoButton"):
当我单击按钮,我想从上一个下拉列表中获取当前选定的值和文本,
$(".addToPortalButton").live('click', function (e) {
// grab the previous dropdown list value and text here.
});
使用 jquery 执行此操作的最简单方法:
这是
<select class="addToList" id="Teams" name="Teams">
<option></option>
<option value="49">Team 1</option>
<option value="22">Team 2</option>
</select>
<input type='button' class="addToButton" value='Add to' />
<select class="addToList" id="Teams" name="Teams">
<option></option>
<option value="49">Team 1</option>
<option value="22">Team 2</option>
</select>
<input type='button' class="addToButton" value='Add to' />
<select class="addToList" id="Teams" name="Teams">
<option></option>
<option value="49">Team 1</option>
<option value="22">Team 2</option>
</select>
<input type='button' class="addToButton" value='Add to' />
i have the following code where i have a dropdown list (with class="addToList" and followed by a button (Class="addtoButton"):
When i click on the button, i want to grab the current selected value and text from the previous dropdown list.
$(".addToPortalButton").live('click', function (e) {
// grab the previous dropdown list value and text here.
});
what is the easiest way to doing this using jquery.
Here is the html:
<select class="addToList" id="Teams" name="Teams">
<option></option>
<option value="49">Team 1</option>
<option value="22">Team 2</option>
</select>
<input type='button' class="addToButton" value='Add to' />
<select class="addToList" id="Teams" name="Teams">
<option></option>
<option value="49">Team 1</option>
<option value="22">Team 2</option>
</select>
<input type='button' class="addToButton" value='Add to' />
<select class="addToList" id="Teams" name="Teams">
<option></option>
<option value="49">Team 1</option>
<option value="22">Team 2</option>
</select>
<input type='button' class="addToButton" value='Add to' />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用以下代码来执行此操作:
然后可以使用
$el.val()
和$el.text()
分别获取值和文本。You can do this with the following code:
You can then use
$el.val()
and$el.text()
to get the value and text respectively.您可以使用
.prev()
或.prevAll()
获取之前的编辑:对于较新版本的 jQuery,其中
.live()
已被弃用,新的.on()
语法是:旧版本:
You can use
.prev()
or.prevAll()
to get the<select>
before like this:Edit: for newer versions of jQuery where
.live()
has been deprecated, the new.on()
syntax is:Older version: