setAttribute在JavaScript中不起作用

jav*_*guy 2 html javascript

我正在使用setAttribue如下。它仅在第一次工作,之后显示变化的值,alert但不显示document.getElementById("to").setAttribute("value", selValue);

document.getElementById("listcontact").onchange = function () {
    var selIndex = document.getElementById("listcontact").selectedIndex;
    var selValue = document.getElementById("listcontact").options[selIndex].innerHTML;
    var contactVal = selValue.split(';');      
    var phone = contactVal[2];  

    alert(phone);
    document.getElementById("to").setAttribute("value", selValue);
    selIndex = "";
    selValue = "";
    phone = "";
    selValue = "";
};
Run Code Online (Sandbox Code Playgroud)

为什么这不能按我预期的那样工作,我该如何解决?

Que*_*tin 5

value属性设置初始值,而不是当前值。

value属性分配一些内容。

document.getElementById("to").value = selValue;
Run Code Online (Sandbox Code Playgroud)