使用jQuery查找所选选项的名称 [英] Find name of selected option using jQuery

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

问题描述

我已经创建了一个jquery / ajax函数来更新#courses,发送#fos的.val()和.text(),特别是选择的那个,如下所示:

I've made a jquery/ajax function that updates #courses, sending #fos's .val() and .text(), specifically of the one that is selected, like so:

$('#selling #fos').change(function() {
    $.post('/ajax/courses',
        {
            fos_id: $('#selling #fos').val(),
            name: $('#selling #fos :selected').text()
        },
    function(data) {
        $('#selling #courses').html(data);
    });
});

如何扩展此功能以便它使用'this',允许我重复使用此函数在同一页面上的时间?我被抓住是因为你不能使用 name:$(this +':selected')。text()

How do I extend this function so that it uses 'this', allowing me to reuse this function multiple times on the same page? I'm caught because you can't use name: $(this + ' :selected').text().

推荐答案

这应该有效:

$("#"+$(this).attr("id")+" :selected")

它不漂亮但是确实如此诀窍:))

it's not pretty but it does the trick :)

或者这将有效:

$(this).find(":selected").text()

这篇关于使用jQuery查找所选选项的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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