2015-02-11 71 views
-1

這可能很簡單,但使用谷歌搜索並沒有幫助我。所以我有一個連接到網絡的機器人,它響應對URL的查詢,例如http://172.16.0.1:8001/web/Speed?Speed1=100停止我的HTML按鈕離開頁面

我想要做的就是製作一些簡單的按鈕,它將發送這些查詢但不會離開頁面。目前我有以下HTML工作,但每次按下按鈕時,它都會轉到相關URL並離開主頁面。

<!DOCTYPE html> 
<html> 
    <body> 

    <strong>MyRIO Robot Controller</strong> 


    <input type=button onClick="parent.location='http://172.16.0.1:8001/web/connect?Connect=1'" value='Start Control'> 
    <input type=button onClick="parent.location='http://172.16.0.1:8001/web/connect?Connect=0'" value='Stop Control'> 
    <input type=button onClick="parent.location='http://172.16.0.1:8001/web/Speed?Speed1=100'" value='Full Speed'> 
    <input type=button onClick="parent.location='http://172.16.0.1:8001/web/Speed?Speed1=50'" value='Half Speed'> 
    <input type=button onClick="parent.location='http://172.16.0.1:8001/web/Speed?Speed1=00'" value='Stop'> 

    </body> 
</html> 
+4

如果你不想在POST或GET時點擊按鈕,你必須做AJAX。 – jrummell 2015-02-11 14:33:49

+1

只要Ajax在同一個域上,您的解決方案就是您的解決方案,否則包含頁面作爲iframe也是可能的。 – Spectator 2015-02-11 14:35:21

+0

好吧我現在正在某個地方,你有我可以使用的一些AJAX的例子(我什至不知道AJAX是什麼,但我願意學習) – lilSebastian 2015-02-11 14:35:52

回答

0

因此,AJAX似乎並不那麼難學。對於有這個問題的任何人在這裏是我想出的解決方案

<!DOCTYPE html> 
<html> 
<head> 
<script> 
    function startControl() 
    { 
     var xmlhttp; 
     if (window.XMLHttpRequest) 
     {// code for IE7+, Firefox, Chrome, Opera, Safari 
      xmlhttp=new XMLHttpRequest(); 
     } 
     xmlhttp.onreadystatechange=function() 
     { 
      if (xmlhttp.readyState==4 && xmlhttp.status==200) 
      { 
       document.getElementById("myDiv").innerHTML=xmlhttp.responseText; 
      } 
     } 

     xmlhttp.open("GET","http://172.16.0.1:8001/web/connect?Connect=1",true); 
     xmlhttp.send(); 
    } 
    function stopControl() 
    { 
     var xmlhttp; 
     if (window.XMLHttpRequest) 
     {// code for IE7+, Firefox, Chrome, Opera, Safari 
      xmlhttp=new XMLHttpRequest(); 
     } 
     xmlhttp.onreadystatechange=function() 
     { 
      if (xmlhttp.readyState==4 && xmlhttp.status==200) 
      { 
       document.getElementById("myDiv").innerHTML=xmlhttp.responseText; 
      } 
     } 

     xmlhttp.open("GET","http://172.16.0.1:8001/web/connect?Connect=0",true); 
     xmlhttp.send(); 
    } 
</script> 
</head> 
<body> 

<h2>MyRIO Robot Controller</h2> 
<button type="button" onclick="startControl()">Start Control</button> 
<button type="button" onclick="stopControl()">Stop Control</button> 
<div id="myDiv"></div>