开发者

How to get selected value in combo box?

开发者 https://www.devze.com 2023-03-23 16:20 出处:网络
If I have a form like this. <form name=\"myform\" action=\"\" method=\"POST\"> <select name=\"mydropdown\">

If I have a form like this.

<form name="myform" action="" method="POST">
<select name="mydropdown">
<option value="A">AAA</option>
<option value="B">BBB</option>
<option value="C">CCC</option>
</select>
</form>

For radio buttons I would do

var type = $(this).find('input:radio[name="ctype"]:checked').val() || '';

but this can't I get to work on combo boxes.

How can I get the value of the selected option in a combo box?

Update

It is开发者_运维技巧 called on a webpage with many forms, so I need the selected value from this particular combo box.

Here is how I get the values from text boxes and radio buttons.

$('form').live('submit', function(){

   var title = this.elements.title.value;
   var type  = $(this).find('input:radio[name="ctype"]:checked').val() || '';
   var sect  = ?

   ...


Simply :

$('select[name="mydropdown"]').val();

UPDATE:

And when inside one of the forms :

$('form').live('submit', function(){

   var value_of_dropdown  = $(this).find('select[name="mydropdown"]').val();

   ...


For select boxes its enough to use val() to get the selected value.

$('select[name="mydropdown"]').val();

EDIT: I edited the response because I could have sworn there was an id named mydropdown :)


$('select[name="mydropdown"] option:selected').val()

UPDATE:

    $('form').live('submit', function(){

       var value_of_dropdown  = $(this).find('select[name="mydropdown"] option:selected').val();

       ...
  });


Using jQuery, just go for it like:

$('select').val();


$('select[name="mydropdown"]').change(function() {
   $('select[name="mydropdown"]').val();
});


Get value selected with javascript:

var countsw=document.getElementById("controlid").options[document.getElement.getElementById("controlid").selectedIndex].value;  

With this code you can get value selected combobox even if client-side control has this attribute runat="server" that use in server-side.

0

精彩评论

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