jQuery .change() get :selected 文本值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12279904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:31:05  来源:igfitidea点击:

.change() get :selected text value

jqueryselected

提问by ngplayground

I'm trying to run the following code

我正在尝试运行以下代码

$("select#shipping_rates_drop").change(function(){
            var selectedText = $(this + "option:selected").text();
            var ship = thisvalue.split("£");
            var subtotal = $("ul#deliveryList").attr("class");
            var ship = ship[1];
            var totalcost = parseFloat(subtotal)+parseFloat(ship);
            $(".wrap.form-section h1 span").html("&nbsp;&nbsp;&nbsp;&nbsp;<small>Total </small> £"+totalcost);
            $("input[name=itemamt]").val(subtotal);
            $("input[name=shippingamt]").val(ship);
            checklistCheck1();
        });

I want to get the text from the selected value on change. ex. UPS Worldwide Express - £89.57 then the code splits the value to get me the actual number of cost.

我想在更改时从选定的值中获取文本。前任。UPS Worldwide Express - £89.57 然后代码拆分价值以获得我的实际成本数量。

But console.log throws up the following

但是 console.log 抛出以下内容

Error: Syntax error, unrecognized expression: [object HTMLSelectElement]option:selected

错误:语法错误,无法识别的表达式:[object HTMLSelectElement]option:selected

in jquery.min.js (line 2)

在 jquery.min.js(第 2 行)中

So im assuming I've done something wrong here and hoping someone could help out

所以我假设我在这里做错了什么,希望有人能帮忙

回答by Loken Makwana

the line should be

这条线应该是

var selectedText = $(this).find("option:selected").text();

回答by psynnott

Change

改变

var thisvalue = $(this + "option:selected").text();

to

var thisvalue = $("select#shipping_rates_drop option:selected").text();

回答by Hasib Kamal

Selected text value :

选定的文本值:

$('#selectBoxId').on('change',function(){
   var optionsText = this.options[this.selectedIndex].text;
   alert(optionsText);
});

回答by J.Mas

<select onchange="changeSelect($event)">
    <option [value]="valor">Texto</option>
</select>
changeSelect(e: any) {
    var indexSel = e.target.selectedIndex;
    var label = e.srcElement[indexSel].label;console.log(label);
}