Get selected option from select element

后端 未结 12 983
温柔的废话
温柔的废话 2020-12-08 01:45

I am trying to get the selected option from a dropdown and populate another item with that text, as follows. IE is barking up a storm and it doesn\'t work in Firefox:

<
12条回答
  •  忘掉有多难
    2020-12-08 02:14

    The first part is to get the element from the drop down menu which may look like this:

    
    

    To capture via jQuery you can do something like so:

    $('#cycles_list').change(function() {
        var mylist = document.getElementById("cycles_list");
        iterations = mylist.options[mylist.selectedIndex].text;
    });
    

    Once you have stored the value in your variable, the next step would be to send the information stored in the variable to the form field or HTML element of your choosing. It may be a div p or custom element.

    i.e.

    OR

    You would use:

    $('p').html(iterations); OR $('div').html(iterations);

    If you wish to populate a text field such as:

    You would use:

    $('#textform').text = iterations;

    Of course you can do all of the above in less steps, I just believe it helps people to learn when you break it all down into easy to understand steps... Hope this helps!

提交回复
热议问题