将jQuery选项值转换为非对象


Convert jQuery option value to a non-object

我试图将一个选项的var文本传递给jquery中的加载事件,但代码没有返回选项值的文本,我相信它正在返回一个对象。如何获得选项选择的字符串值?下面是到目前为止的代码

var statevar = $("#stateselect option:selected").val();

$( "#stateselect").change(function() {
console.log($( "#stateselect option:selected").val());
$( "#FactoryPricing" ).load("/include/StateSelection.php"+"?stateSELECT="+statevar);
});
});

php变量stateSELECT=在尝试调用该变量时返回空白。

试试这个:

console.log( $("#stateselect option:selected").text() );

<option value="3" selected="selected">Three</option>
$("...... option:selected").text(); // returns "Three"
$(this).val(); // returns "3"

你可能在找这个:

$( "#FactoryPricing" ).load("/include/StateSelection.php"+"?stateSELECT="+this.value);

:

$(function() {
    var statevar = ""; 

    $( "#stateselect").change(function() {
        statevar = this.value;
        $( "#FactoryPricing" ).load("/include/StateSelection.php"+"?stateSELECT="+statevar);
    });
});