简体   繁体   中英

quick javascript question about a dropdown with onchange action

Using smarty, take a look at the code below:

            <select name="unitSelect" class="uniForm" id="unitSelect" style="width:1250px;" onchange="location.href='{$ABS_MANAGER_URL}/managerEditUnit.php?unit_id={$set[i].id}'; return false">
                    <option value="0" selected="selected">Select Unit</option>
                {section name=i loop=$set}
                    <option value="{$set[i].id}" >{$set[i].unitName}</option>
                {/section}
            </select>

What I want, is for the value in the options to load into the select onchange"" call, for the GET variable... I can't figure out how to do this with javascript...any ideas anyone?

Thanks.


UPDATE

It wasn't easy, but between a few of your comments I got one working, thanks all:

<script type="text/javascript">
    function viewUnit(value) {

        var url = "managerEditUnit.php?unit_id="; 
        url += value;
        document.location.href=url;
    }
</script> 

Here is an example:

<select onchange="alert(this.options[this.selectedIndex].value)">
    <option value="0">Option 1</option>
    <option value="1">Option 2</option>
    <option value="2">Option 3</option>
</select>

EDIT:

Sorry for the duplicate.

If you want to use it with a function:

function yourfunction(value) {
    alert(value);
}

And ofcourse change alert in the above HTML to yourfunction .

You could get the value in the select object by doing this in the onchange event:

onchange="alert(this.options[this.selectedIndex]"

Since you can get that value, then you can dynamically build up your GET request. Some like this:

onchange="document.location.href='somepage.php?id=' this.options[this.selectedIndex];"

jQuery makes it even easier, you can just use:

onchange="alert($(this).val())"

应该这样做:

<select onchange="yourfunction(this.options[this.selectedIndex].value);">    

I could suggest you to use following for getting the value when select an option from dropdownlist -->

this.value

moving to your javascript means -->

onchange="JavaScript:userDefinedFunction(this.value);"

eg::

select name='state' onchange="JavaScript:userDefinedFunction(this.value);"

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