简体   繁体   中英

Calling the value of an HTML element as an onclick of another element

I have a select box that calls a function on change.

<select name ="selectBox" class="select_box" onchange="getVal(this)">   

I would like to call this value again upon the change of a radio choice.

<input type="radio" name="orderBy" value="desc" class="radio_sort" onchange="getVal(this)"> Descending<br>
<input type="radio" name="orderBy" value="asc" class="radio_sort" onchange="getVal(this)"> Ascending<br>

This doesn't work. How can I achieve this? Thanks in advance.

Rather than passing this you may as well just always print or do whatever you need to do with the select box when you run the function. (I have added an id to it for this example).

Unless you need the function to perform different things based on content, passing this is unnecessary in your context.

 function getVal() { var select = document.getElementById("selectBox"); console.log(select.value); } 
 <select id="selectBox" name ="selectBox" class="select_box" onchange="getVal()"> <option>yes</option> <option>no</option> </select> <input type="radio" name="test" onChange="getVal()"/> First <input type="radio" name="test" onChange="getVal()"/> Secon 

This is quite a "minimal" question, as per Hep center ... And probably not "complete".

But try this:

<input type="radio" name="orderBy" value="desc" class="radio_sort" onchange="getVal( $( '[name=\"selectbox\"]' )"> Descending<br>
<input type="radio" name="orderBy" value="asc" class="radio_sort" onchange="getVal $( '[name=\"selectbox\"]' )"> Ascending<br>

The trick is to provide the right element as argument to the function.
;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM