开发者

Jquery.change firing on page load

开发者 https://www.devze.com 2022-12-16 09:10 出处:网络
<la开发者_开发问答bel for=\"AddList\" class=\"locType\">Select a location</label> <select id=\"AddList\">
<la开发者_开发问答bel for="AddList" class="locType">Select a location</label>
<select id="AddList">
     <option value="New">New Address...</option>
</select>

The Js.

$(document).ready(function() {
    //Location Code
    $("#AddList").change(function() {
        var str = "";
        $("#AddList option:selected").each(function() {
            str += $(this).text() + " ";
        });
        alert(str);            
    })
    .change();
});

I'm trying to alert the contents whenever the user selects an option in the combobox. Also, could the code be provided to get the value of the selected option also.

Thank you


You're calling .change(); yourself right after you bind it:

//Location Code
$("#AddList").change(function() {
    /* your logic */           
})
.change(); // right here you're calling it

Getting the value of the option:

$("#AddList option:selected").each(function(){
  var val = $(this).val();
});


By writing .change(); at the end of your code, you are manually firing the change event.

What were you trying to do?


To get the value of the option element, call the val method.

0

精彩评论

暂无评论...
验证码 换一张
取 消