Jump to content


Photo

Jquery select selected value undefined

jquery php ajax

  • Please log in to reply
4 replies to this topic

#1 thugsr

thugsr

    Member

  • Members
  • PipPip
  • 28 posts

Posted 15 October 2013 - 04:51 AM

Hi,

I have problem with this line var value = $("#opstina option:selected").val(); , but only in chrome i get that value is undefined. In FireFox, and IE, works fine, only in chrome i get this warning. Does someone know why? My whole jquery code is 

if ($("#drzava option:selected").length) {
                    $("select#opstina").attr("disabled","disabled");
                    $("select#opstina").html("<option>wait...</option>");
                    var id = $("select#drzava option:selected").attr('value');
                        $.post("select_opstina.php", {id:id}, function(data){
                        $("select#opstina").removeAttr("disabled");
                         alert( "html koji bi trebao biti upisan u select:\n\n" + data );
                        $("select#opstina").html(data);
            }); 
                        if ($("#opstina option:selected").length) {
                        alert('Došao sam pred petlju');
                         var value = $("#opstina option:selected").val();//this is line that is problem
                         alert(value); 
                        var sel = $("#opstina").val(); // this do the same
                        if (sel!=0) {
                              alert('Ušao sam u petlju, i selektovano polje za opštinu je' + sel );
            $("select#mesto").attr("disabled","disabled");
            $("select#mesto").html("<option>wait...</option>");
            var id= $("select#opstina option:selected").attr('value');
            alert(id);
            $.post("select_mesto.php", {id:id}, function(data){
                alert("html koji bi trebao biti upisan u select:\n\n" + data );
                $("select#mesto").removeAttr("disabled");
                $("select#mesto").html(data);
            });
        }
    }

                } 
}


#2 Xaotique

Xaotique

    Advanced Member

  • Members
  • PipPipPip
  • 558 posts
  • LocationPennsylvania, United States
  • Age:21

Posted 15 October 2013 - 06:29 AM

Just select the select, rather than the option.

Tested working in Firefox & Chrome: http://jsfiddle.net/XR6DT/

I'm a Linux user, so I can't really test IE, but it should work as well.

#3 thugsr

thugsr

    Member

  • Members
  • PipPip
  • 28 posts

Posted 15 October 2013 - 06:41 AM

But i need selected value, not change. This should be part with pre-selected value. 



#4 Xaotique

Xaotique

    Advanced Member

  • Members
  • PipPipPip
  • 558 posts
  • LocationPennsylvania, United States
  • Age:21

Posted 15 October 2013 - 11:16 AM

I showed getting the value on change. That way it had a trigger you could play with.

The value is grabbed:
$(this).val()
And $(this) points to $("select") in my example. I only have one select tag, so it's fine to use as a trigger. Although, it's pointing directly to the one that was triggered. If there were more select tags, it would still point to the one you changed in that example.

You were pointing to the option tag, when you can simply call the value of the select element.

#5 thugsr

thugsr

    Member

  • Members
  • PipPip
  • 28 posts

Posted 21 October 2013 - 03:20 AM

My trigger in this case should be selected value. For first two selects i get the value, only for third i cant..






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com