2015-08-03 74 views
0

我正在使用JavaScript獲取文本框值。我的文本框的值是「xxxxx?000-1111」。雖然我想從JavaScript函數中獲取這個值,但我得到的值是[object%20HTMLDivElement] ....誰能告訴我,我在這裏犯了什麼錯誤。在javascript中獲取文本框值

文本框和按鈕,文本框的值被替換爲其他方法,並在頁面中很好地顯示。

function clDoc() { 
      path = document.getElementById('val').value; 
      alert(path); 
      var myWindow = window.open(path, '', 'width=520,height=650'); 
     } 

    <input type="text" id="clDoc" name="myn" value="" /> 
    <input type="button" onClick="clDoc()" value="View" id="groupbtn" /> 

的Javascript

function clDoc() { 
      path = document.getElementById('val'); 
      alert(path); 
      var myWindow = window.open(path, '', 'width=520,height=650'); 
     } 

如果我試着用下面我收到警報爲未定義。

function clDoc() { 
      path = document.getElementById('val').value; 
      alert(path); 
      var myWindow = window.open(path, '', 'width=520,height=650'); 
     } 

回答

6

比較:

path = document.getElementById('val').value; 

,你從HTML元素對象獲得的價值和

path = document.getElementById('val'); 

你在哪裏得到的HTML元素對象本身。


path = document.getElementById('val').value; 

現在,你所得到的值屬性,但你說這是:

[對象HTMLDivElement]

div元素不是文本投入。他們不是用戶可編輯的,也沒有價值。

也許你想得到innerHTML

1

希望這pluker將有助於

Plunker

代碼是

function clDoc() { 
      path = document.getElementById('clDoc').value; 
      alert(path); 
      var myWindow = window.open(path, '', 'width=520,height=650'); 
     } 
+1

是的,被用於錯誤的document.getElementById ID()。 – Benjamin