jQuery 如何在页面加载时获取下拉列表的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11222007/
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 10:21:15  来源:igfitidea点击:

How do I get the value of drop down list on page load

jquerydrop-down-menuonload

提问by bikey77

Using jQuery, how can I get the selected item's value in a select when the page loads? I can do it onChange etc but cant figure out the way on page load.

使用 jQuery,如何在页面加载时在选择中获取所选项目的值?我可以做 onChange 等,但无法弄清楚页面加载的方式。

Here's my code:

这是我的代码:

<select name="symb" id="symb">
  <option value="1#10#6">Basic Personal</option>
  <option selected="selected" value="2#20#6">Basic Family</option>
  <option value="7#90#12">Advanced Personal</option>
  <option value="8#120#12">Advanced Family</option>
  <option value="9#130#12">Optimum Personal</option>
  <option value="10#170#12">Optimum Family</option>
</select>

This is the script I'm using for getting the option value onchange:

这是我用于获取 onchange 选项值的脚本:

$(document).ready(function() {
    $("#symb").change(function() {
          var val = $(this).symbCost(0);
    })

   $.fn.symbCost = function(pos) {
        var total = parseInt($("option:selected").val().split('#')[pos]);
        return total;
    }
});

回答by slash197

$(document).ready(function() {
    alert($("#symb").val());
});

回答by Tats_innit

Working Demohttp://jsfiddle.net/YXbDd/3/orhttp://jsfiddle.net/YXbDd/4/

工作演示http://jsfiddle.net/YXbDd/3/http://jsfiddle.net/YXbDd/4/

API: http://api.jquery.com/ready/> Specify a function to execute when the DOM is fullyloaded.

API:http: //api.jquery.com/ready/ 已> Specify a function to execute when the DOM is fully加载。

I am not sure what poscontext is in your case but this should do the stick.

我不确定pos您的情况是什么背景,但这应该可以解决问题。

$("#symb").val().split('#')[0]

$("#symb").val().split('#')[0]

Rest as demo and code, hope it helps,

作为演示和代码休息,希望它有帮助,

code

代码

$(document).ready(function() {
    alert("when the page load ==> " + $("#symb").val().split('#')[0]);
    $("#symb").change(function() {
          var val = $(this).symbCost(0);
    })

   $.fn.symbCost = function(pos) {
        var total = parseInt($("option:selected").val().split('#')[pos]);
        alert(total);
        return total;
    }
});?

or

或者

$(document).ready(function() {
    alert("when the page load ==> " + $("#symb option:selected").val().split('#')[0]);
    $("#symb").change(function() {
          var val = $(this).symbCost(0);
    })

   $.fn.symbCost = function(pos) {
        var total = parseInt($("option:selected").val().split('#')[pos]);
        alert(total);
        return total;
    }
});?

回答by nilesh

$(document).ready(function() {
    alert($("#dropdownid").val());
});

回答by feralou

The best solution and siple:

最好的解决方案和简单的:

$(document).ready(function() {
$.fn.symbCost = function(pos) {
var total = parseInt($(this).val().split('#')[pos]);
    return total;
}   
$("#symb").change(function() {
    var val = $(this).symbCost(0);
}).triggerHandler('change');

});

});

Or this

或这个

$(document).ready(function() {
$.fn.symbCost = function(pos) {
    var total = parseInt($(this).val().split('#')[pos]);
    return total;
}
$("#symb").change(function() {
    var val = $(this).symbCost(0);
}).symbCost(0);

});

});