2015-02-11 73 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>