How to get selected values from select option in php 8

There are a variety of ways to use JavaScript to lớn obtain the value of the selected option in a select menu, or a reference to the selected option:

Use the value property of the select danh sách. Use the selectedIndex property. Loop through the options & determine which one is selected and return either its value or a reference.

Bạn đang xem: How to get selected values from select option in php 8

We will discuss all of these here, starting with the value property. The following size is used to lớn demonstrate:

Demo: Get Value or Text of Selected Option

Scrolling Divs JavaScript JavaScript Tooltips Continuous Scroller Rotating Banner JavaScript Random Image PHP PHP Form Generator PHP Table Class PHPhường Order Forms

Make a selection from the các mục and click the buttons lớn see the result. The markup & JavaScript for the example are displayed below.

The value Property

The value property of a select box comes from the value attribute of the selected option or the first selected option in a select-multiple type select box. So the value of the selected option can be obtained in JavaScript as follows:

// reference to "scripts" select danh mục // used throughout the examples belowvar sel = document.getElementById("scripts");// display value property of select menu (from selected option)console.log( sel.value );
If option elements bởi not contain a value attribute, generally the text content of the option element will be the value property. However, Internet Explorer prior to lớn version 9 will not provide the value in this case. If your option elements vị not include value attributes & you wish to lớn tư vấn older browsers, for best results use the selectedIndex và text properties.

The selectedIndex Property

The selectedIndex property provides the index number of the selected option in a select menu.

Xem thêm: Rana - Fixed Gear Trick Cơ Bản

<1> You can use it to lớn reference the selected option as follows:

// reference lớn selected optionvar opt = sel.options;
Then you can access the option"s value, text, or other properties:

// display value và text property valuesconsole.log( opt.value );console.log( opt.text );
The options property of the select các mục is a node danh mục of all the option elements contained within it. The selectedIndex property of the select danh sách specifies the location of the selected option in the node danh mục. The text property of an option is the nội dung of the option element.

Xem thêm: Evfta Là Gì — TiếNg ViệT, Chừng Nào Ta Được Mua Xe Hơi Với Mức Thuế 0 Đồng

Using a for Loop khổng lồ Obtain Selected Option

You can use a for loop khổng lồ iterate through the options of a select menu to lớn determine which is selected. A function can be defined to return a reference khổng lồ the selected option or its value. The following returns a reference to the selected option:

function getSelectedOption(sel) var opt; for ( var i = 0, len = sel.options.length; i len; i++ ) opt = sel.options; if ( opt.selected === true ) break; return opt;// get selected option in sel (reference obtained above)var opt = getSelectedOption(sel);// display its value và textconsole.log( opt.value );console.log( opt.text );
This function returns a single selected option, which is suitable for a select-one type select danh mục. A similar function suited for select-multiple type select lists can return multiple selected options.

Example Form Markup

Markup for the example khung above is displayed here:

size action="#" method="post" id="demoForm" class="demoForm"> fieldset> legend>Demo: Get Value or Text of Selected Optionlegend> p> select id="scripts" name="scripts"> option value="scroll">Scrolling Divs JavaScriptoption> option value="tooltip">JavaScript Tooltipsoption> option value="con_scroll">Continuous Scrolleroption> option value="banner">Rotating Banner JavaScriptoption> option value="random_img">Random Image PHPoption> option value="form_builder">PHP Form Generatoroption> option value="table_class">PHPhường Table Classoption> option value="order_forms">PHPhường Order Formsoption> select> input đầu vào type="text" size="30" name="display" id="display" /> p> p> đầu vào type="button" id="showVal" value="Value Property" /> đầu vào type="button" id="showTxt" value="selectedIndex/Text" /> đầu vào type="button" id="doLoop" value="Value from Loop" /> p> fieldset>form>

JavaScript for Example Form

JavaScript for the example is displayed here:

(function() // get references lớn select danh mục và display text box var sel = document.getElementById("scripts"); var el = document.getElementById("display"); function getSelectedOption(sel) var opt; for ( var i = 0, len = sel.options.length; i len; i++ ) opt = sel.options; if ( opt.selected === true ) break; return opt; // assign oncliông chồng handlers lớn the buttons document.getElementById("showVal").onclichồng = function () el.value = sel.value; document.getElementById("showTxt").oncliông chồng = function () // access text property of selected option el.value = sel.options.text; document.getElementById("doLoop").oncliông chồng = function () var opt = getSelectedOption(sel); el.value = opt.value; ());// immediate function khổng lồ preserve sầu global namespace
Baông xã to top

Chuyên mục: