2015-09-28 62 views
0

我曾经在我的输入字段的onclick方法加载功能如何从复选框选中时

<td>&nbsp;<input type="checkbox" name="checkbox1" onclick="load();" value="Bike">Include previous service terms</td> 

这里删除值函数

<script type ="text/javascript"> 

     function load() { 
      if (window.XMLHttpRequest) { 
       xmlhttp = new XMLHttpRequest(); 
      } else { 
       xmlhttp = new ActiveXObject('Microsoft.XMLHTTP'); 
      } 

      xmlhttp.onreadystatechange = function() { 
       if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
        document.getElementById('show').innerHTML = xmlhttp.responseText; 
       } 
      } 
      xmlhttp.open('GET','include.inc.php', true); 
      xmlhttp.send(); 
     } 

    </script> 

但我想不出如何删除复选框时显示的值。我已经尝试了以前提出的问题的答案,但它显示了我的NetBeans中的语法错误

+0

轻微的建议,你可以看看使用jQuery。在我看来,使AJAX变得更容易。 –

+0

克蒙人,不要那样做。它甚至不是ajax问题 – Jesse

回答

0

由于您在此提供了一些XHR代码,因此我假定您的意思是在未选中该框时如何清除show元素。如果这是不正确的假设,请纠正我。

请试试下面的代码。如果在调用加载函数时未选中此框,则会清除元素innerHTML。

function load() { 
 
      if(!document.getElementById('checkbox1').checked){ 
 
       return document.getElementById('show').innerHTML=''; 
 
      } 
 
      if (window.XMLHttpRequest) { 
 
       xmlhttp = new XMLHttpRequest(); 
 
      } else { 
 
       xmlhttp = new ActiveXObject('Microsoft.XMLHTTP'); 
 
      } 
 

 
      xmlhttp.onreadystatechange = function() { 
 
       if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
 
        document.getElementById('show').innerHTML = xmlhttp.responseText; 
 
       } 
 
      } 
 
      xmlhttp.open('GET','include.inc.php', true); 
 
      xmlhttp.send(); 
 
     }
<td>&nbsp;<input type="checkbox" id='checkbox1' name="checkbox1" onclick="load();" value="Bike">Include previous service terms</td> 
 

 
<div id='show'>I have text</div>

在上面的例子中,你可以在控制台中看到日志XHR时,选中此复选框正在作出(当然不及格),而不是当没有选中该复选框正在取得进展。

+1

谢谢!有效! – xiongmao

+0

快乐编程! – Jesse