setAttribute在JavaScript中不起作用 [英] setAttribute is not working in JavaScript

查看:174
本文介绍了setAttribute在JavaScript中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 setAttribue 如下。它的工作只是第一次,之后改变的值显示警报,但没有用 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 =;
};

为什么这不像我期望的那样工作,我该如何解决它?

解决方案

属性设置初始值值, em> current value。



属性指派一些东西。

  document.getElementById(to)。value = selValue; 


I am using setAttribue as below. Its working only for first time and after that the changing value is showing the alert but not setting with 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 = "";
};

Why is this not working as I expect and how can I fix it?

解决方案

The value attribute sets the initial value, not the current value.

Assign something to the value property instead.

document.getElementById("to").value = selValue;

这篇关于setAttribute在JavaScript中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆