2015-09-04 37 views
-2

如果在在的onclick的函數,我說:爲什麼我不能連接這些JS函數?

offset=parseFloat(document.getElementById("offsetId").value); //value=-2 
center=sp+offset; // sp is a float == 190 
alert("center="+center); 

我得到 「中心= 190 [對象HTMLInputElement]」

如果我分離的功能爲:

offset=document.getElementById("offsetId"); 
offset=parseFloat(offset.value); 
center=sp+offset; 
alert("center="+center); 

我得到相同的錯誤。

但如果我這樣做,這樣

offset=document.getElementById("offsetId"); 
center=sp+parseFloat(offset.value); 
alert("center="+center); 

我得到正確的答案,中心= 188。

爲什麼我不能像前兩個例子那樣連接函數? 他們看起來和我一樣。

編輯: 這裏是一個生產的「不可能」的結果示例代碼:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<title> New Document </title> 
</head> 

<body> 
<script>sp=parseFloat(190.); </script> 
<!-- same result with sp=190.; sp=Number(190.); sp=Number("190."); --> 

<form> 
    <p>Enter Offset <input id="offsetId" type="text" name="offset" value=0> <!-- eg -2 --> 
    <p> 
    <input 
    type="button" 
    name="go" 
    value="Do the Plot" 
     onclick=' 
      alert("at19: sp = "+sp+", typeof sp=", typeof sp); 
          // shows: at19: sp = 190, typeof sp= 
      offset=parseFloat(document.getElementById("offsetId").value); 
      alert("at22, offset="+offset+", typeof offset="+typeof offset); 
    // shows: at23, offset=[object HTMLInputElement], typeof offset=object 
      center=sp+offset; 
      alert("at25: center="+center); 
      // shows: at24: center=190[object HTMLInputElement] 

// alternate 1 
      offset = parseFloat(offset.value); 
      center=sp+offset; 
      alert("at31: offset="+offset+", center="+center); 
// shows: at31: offset=[object HTMLInputElement], center=190[object HTMLInputElement] 

// alternate 2 
      offset=document.getElementById("offsetId"); 
      center=sp+parseFloat(offset.value); 
      alert("at37: offset="+offset+", center="+center); 
// shows: at36: offset=[object HTMLInputElement], center=188 
     ' 
     > 
    </form>  
</body> 
</html> 

注意最後一個方法給出了正確的答案。 我懷疑我不能讓sp顯示一個類型可能很重要。

+2

既不[第一](https://jsfiddle.net/pdk6L6r4/)或[秒](https://jsfiddle.net/pdk6L6r4/1/)實例產生輸出你聲稱它。 'parseFloat'不可能返回一個HTML元素,或者一個代表HTML元素的字符串,或者任何其他可以在你的問題中產生輸出的值。請包含可重現您問題的工作示例。使用內置的代碼編輯器。 – meagar

+0

我舉了一個例子(現在是一個完整的程序),它給出的輸出如此不尋常,以至於你聲稱這是不可能的,你認爲這不值得發佈?請看看我發佈的程序,以及「不可能」的警報輸出。 – user1067305

+0

責任在於你提供代碼來重現你的問題,而你**沒有做到這一點。現在您已經生成了可以**重現您問題的代碼,問題和解決方案顯而易見。 – meagar

回答

0

你還沒有聲明你的變量,所以offset是不是你認爲它是。

在您的事件處理程序的上下文中,offset是對已經由窗體定義的<input name="offset">的引用。

如果您要用現有變量的同名變量來映射現有變量,則需要使用var offset來聲明它。

<script>sp=parseFloat(190.); </script> 
 
<!-- same result with sp=190.; sp=Number(190.); sp=Number("190."); --> 
 

 
<form> 
 
    <p>Enter Offset <input id="offsetId" type="text" name="offset" value=0> <!-- eg -2 --> 
 
    <p> 
 
    <input 
 
    type="button" 
 
    name="go" 
 
    value="Do the Plot" 
 
     onclick=' 
 
      var offset = parseFloat(document.getElementById("offsetId").value); 
 
      alert("at22, offset="+offset+", typeof offset="+typeof offset); 
 
     ' 
 
     > 
 
    </form> 

相關問題