简体   繁体   English

关于onchange操作下拉菜单的快速JavaScript问题

[英]quick javascript question about a dropdown with onchange action

Using smarty, take a look at the code below: 使用smarty,请看下面的代码:

            <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? 我想要的是将选项中的值加载到select onchange“”调用中,对于GET变量...我不知道如何使用javascript ...有什么想法吗?

Thanks. 谢谢。


UPDATE 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 . 当然可以将以上HTML中的alert更改为您的yourfunction

You could get the value in the select object by doing this in the onchange event: 您可以通过在onchange事件中执行以下操作来在select对象中获取值:

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

Since you can get that value, then you can dynamically build up your GET request. 由于可以获得该值,因此可以动态建立GET请求。 Some like this: 像这样:

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

jQuery makes it even easier, you can just use: jQuery使它变得更加容易,您可以使用:

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 --> 移到您的javascript意味着->

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

eg:: 例如::

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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