开发者

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">
    functio开发者_如何学Pythonn 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())"


This should do it:

<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);"
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜