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

后端 未结 4 1259
星月不相逢
星月不相逢 2021-01-13 07:00

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.

Here\'s my

4条回答
  •  没有蜡笔的小新
    2021-01-13 07:52

    Working Demo http://jsfiddle.net/YXbDd/3/ or http://jsfiddle.net/YXbDd/4/

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

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

    $("#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;
        }
    });​
    

提交回复
热议问题