2012-06-29 8 views
2

单选按钮代码有什么问题? 当我试过SELECT/OPTION它正在工作。如何在ajax中使单选按钮 - php?

我想使两组单选按钮,得到它们的值和总结。 但我无法移动,因为单选按钮根本没有响应。

这些都是投入到我的表格:

<input type='radio' id='1' name='r' onclick='pieces('this.value')' 
     autocomplete='off' value='yes'>Yes 
<input type='radio' id='2' name='r' onclick='pieces('this.value')' 
     autocomplete='off' value='no'>No 

我的脚本:

<script type='text/javascript'> 

    function pieces(str) 
    { 

     if (str=='') 
     { 
      document.getElementById(abc).innerHTML=''; 
      return; 
     } 

     if (window.XMLHttpRequest) 
     { 
      // code for IE7+, Firefox, Chrome, Opera, Safari 
      xmlhttp=new XMLHttpRequest(); 
     } 
     else 
     { 
      // code for IE6, IE5 
      xmlhttp=new ActiveXObject('Microsoft.XMLHTTP'); 
     } 

     xmlhttp.onreadystatechange=function() 
     { 
      if (xmlhttp.readyState==4 && xmlhttp.status==200) 
      { 
       document.getElementById(abc).innerHTML=xmlhttp.responseText; 
      } 
     } 

     xmlhttp.open('GET','price.php?q='+str,true); 
     xmlhttp.send(); 

    } 
</script> 

回答

2

下面是修改后的脚本。 输入:

<input type='radio' id='1' name='r' onclick="pieces(this.value);" 
     autocomplete='off' value='yes'>Yes 
<input type='radio' id='2' name='r' onclick="pieces(this.value);" 
     autocomplete='off' value='no'>No 

这里是js脚本:

function pieces(str) 
    { 

     if (str=='') 
      { 
      document.getElementById("abc").innerHTML='Choose something'; 
      return; 
      } 

     if (window.XMLHttpRequest) 
      {// code for IE7+, Firefox, Chrome, Opera, Safari 
      xmlhttp=new XMLHttpRequest(); 
      } 
      else 
       {// code for IE6, IE5 
       xmlhttp=new ActiveXObject('Microsoft.XMLHTTP'); 
       } 

     xmlhttp.onreadystatechange=function() 
      { 
      if (xmlhttp.readyState==4 && xmlhttp.status==200) 
       { 
       document.getElementById("abc").innerHTML=xmlhttp.responseText; 
       } 
      } 

     xmlhttp.open('GET','hey.php?q='+str,true); 
     xmlhttp.send(); 

    } 

让我知道如果任何概率。

Jasminder

+0

感谢它的工作原理...希望你能回答我的跟进问题:) –

+1

如果一切正常,赞成票和标记@jasminder回答是正确的。提出一个新问题,不要发布后续问题作为答案。 –