2016-03-08 28 views
0

我試圖編寫這個網絡應用程序,讓用戶輸入有關他們的設備的重要信息,然後將其放置在JavaScript中的相關位置。這是我的代碼到目前爲止。使用ID獲取用戶輸入元素

<script type="text/javascript"> 
     function update() { 
      var key = document.getElementById("key").value; 
      if (input.length < 40) { 
       alert("Please enter a valid input"); 
       return; 
      } 
      document.getElementById("access key").innerHTML; 
     } 
</script> 
<script type="text/javascript"> 
    function update() { 
     var device_id = document.getElementById("device_id").value; 
     if (input.length < 24) { 
      alert("please enter a valit input"); 
      return; 
     } 
     document.getElementById("device_id").innerHTML; 
    } 

    </script> 
<p><input type="text" id="key" autofocus placeholder = "Enter product key here" /></p> 
<p><input type="text" id="device_id" autofocus placeholder = "Enter device ID here" /></p> 
<p><input type="submit" value="Submit" onclick="update()"/></p> 


    <h1>Rotary Gate Systems</h1> 



    <article> 
     <a href="#" class="button open_button">Open</a> 
    </article> 
    <article> 
     <a href="#" class="button close_button">Close</a> 
    </article> 

    <article> 
     <p class="status_closed status_button">CLOSED</p> 
     <p class="status_open status_button">OPEN</p> 
     <p class="status_none status_button">NO CONNECTION</p> 
    </article> 



    <script src='http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.js'></script> 
    <script>/*jslint browser: true*/ 
/*global $, jQuery*/ 
/*jshint strict: true */ 

/*EDIT THESE VARIABLES*/ 
//key is the same as your 'access token' 
var key = "key" 

//device_id is the same as the 'core id' 
var device_id = "device_id" 

我想我可能會錯過提交按鈕中的某些東西,或者我想要做的事可能無法使用此屬性。有人可以看看這個,讓我知道我可能會搞砸嗎?

+0

什麼你期望'的document.getElementById( 「快捷鍵」)的innerHTML做;'怎麼辦? – Thriggle

+0

我的理論是改變代碼底部的「var key」和「var device_id」。 –

回答

1

你需要改變你的條件語句,

if (key.length < 40) { 

if (device_id.length < 40) { 

因爲input沒有內部的update()功能定義。

此外,您應該考慮將兩個update()函數合併爲一個函數,以便在提交表單時一起對keydevice_id進行有效性檢查。

+0

我會進行這些更改,感謝您的輸入。你看到提交按鈕有什麼問題嗎?我不確定它是否正確,這是我第一次使用這種方法編輯javascript。 –

0

根據你的意見,這聽起來像你試圖用兩個<input>元素的值更新keydevice_id變量。

您應該瞭解JavaScript中的變量範圍,以確保您可以訪問這些變量。

如果keydevice_id變量在函數運行時的範圍內,則可以直接爲它們賦值。不要在var關鍵字前分配任務,否則您將定義新的本地範圍變量,而不是更新外部範圍中的現有變量。

您也不能在同一範圍內定義兩個具有相同名稱的函數(在本例中爲update)只有最近定義的功能纔有效。

var key = "key"; 
 
var device_id = "device_id"; 
 

 
function update() { 
 
    var tempkey = document.getElementById("key").value; 
 
    if (tempkey.length < 40) { 
 
    alert("Please enter a valid key."); 
 
    return; 
 
    }else{ 
 
    key = tempkey; 
 
    } 
 
    tempdevice_id = document.getElementById("device_id ").value; 
 
    if (tempdevice_id.length < 24) { 
 
    alert("please enter a valid device ID."); 
 
    return; 
 
    }else{ 
 
    device_id = tempdevice_id; 
 
    } 
 
}
<p> 
 
    <input type="text" id="key" autofocus placeholder="Enter product key here" /> 
 
</p> 
 
<p> 
 
    <input type="text" id="device_id" autofocus placeholder="Enter device ID here" /> 
 
</p> 
 
<p> 
 
    <input type="submit" value="Submit" onclick="update()" /> 
 
</p> 
 
<h1>Rotary Gate Systems</h1> 
 
<article> 
 
    <a href="#" class="button open_button">Open</a> 
 
</article> 
 
<article> 
 
    <a href="#" class="button close_button">Close</a> 
 
</article> 
 
<article> 
 
    <p class="status_closed status_button">CLOSED</p> 
 
    <p class="status_open status_button">OPEN</p> 
 
    <p class="status_none status_button">NO CONNECTION</p> 
 
</article>