在html中使用dropdownlistbox

时间:2011-09-13 12:28:25

标签: javascript html

我在html页面中使用了dropdownlist和一个标签。我想更改标签名称 选择我的下拉列表框。这怎么可能?

请为我提供任何帮助。

3 个答案:

答案 0 :(得分:1)

    <select id='derp' onchange="changeVal(this,'changeme');">  <!-- declare select, set onchange to point to changeVal JS -->
        <option value='test'>test</option>
    </select>
    <input id='changeme' />
    <script type='text/javascript'>

        function changeVal(el,changeElID){

            var changeEl = document.getElementById(changeElID); // Get input to change
            changeEl.value = el.options[el.selectedIndex].value; // Change input value to value of selected index

        }
    </script>

[编辑] 重新阅读这个问题听起来你正在尝试更改输入框的名称...如果是这种情况,请将changeEl.value更改为changeEl.name

答案 1 :(得分:1)

检查this example

它将更改标签的属性“name”及其文本内容(读取其新名称属性值)以查看有效更改。

<script type="text/javascript">
function changeValue(select) {

  var value =  select.options[select.selectedIndex].value;
  var label =  document.getElementById('mylabel');

  label.setAttribute('name', value);
  label.innerHTML = label.getAttribute('name');

} 
</script>

<select name="selectcity" onchange="changeValue(this);" >
    <option>Mumbai</option>
    <option>Delhi</option>
</select>
You selected: <label id="mylabel" name="citylabel"></label>

答案 2 :(得分:0)

<强> HTML:

<table cellspacing="0" cellpadding="1" rules="all" border="0" id="gvLanguage">
    <tr>
        <td align="left">
            <select id="ddlLanguage" style="width: 230px;">
                <option value="0">[Select]</option>
                <option value="1">Afrikaans</option>
                <option value="2">Akan</option>
                <option value="3">Albanian</option>
                <option value="4">American</option>
            </select>
        </td>
    </tr>
    <tr>
        <td>
            Selected Value: <label id="lblChange" ></label>
        </td>
    </tr>
</table>

<强> JQUERY:

$(document).ready(function(){
    $("#ddlLanguage").change(function(){
        var vSelectedValue  = $("option:selected",$(this)).text();
       $("#lblChange").text(vSelectedValue);
    });
});

CLICK HERE TO SEE THE DEMO