像這樣是獲取不到值的,彈出的消息是 underfined:
<html>
<style type="text/css">
input {
border: 1px solid #CCCCCC;
font-size: 10.5pt;
padding: 2px 3px 1px;
width: 220px;
color: #999999;
}
</style>
</head>
<body>
<input name="myInput-keleyi-com" id="keleyiInput" type="text" onBlur="(this.value=='')?this.value='123456':this.value" onFocus="abc()" value="123456" >
<script>
function abc(){
var x=document.getElementsByName("myInput-keleyi-com").value;
alert(x);
}</script>
</body>
</html>
應該是這樣:
var x=document.getElementsByName("myInput-keleyi-com")[0].value;
或者:var x=document.getElementsById("keleyiInput").value;
getElementById是element單數,
getElementsByName是elements復數。