如何使用while循环在onchange中获取多个值

时间:2012-11-19 07:17:08

标签: php ajax

我想在while循环中使用onchange从select框中获取值。但我只获得第一个价值。如何从所有人那里获得价值?

我刚刚在php中使用while循环。

有人能告诉解决方案吗?

提前致谢。

这是我的代码: 这是我的代码......

<script type="text/javascript">
function showUser1()
{

var str = document.getElementById("process").value;
var str1 = document.getElementById("snp").value;

alert(str);
alert(str1);

if (str=="")
  {
  document.getElementById("txtHint1").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("txtHint1").innerHTML=xmlhttp.responseText;
    }
  }
//xmlhttp.open("GET","process.php?q="+str,true);
xmlhttp.open("GET","process.php?q=" + str + "&q1=" + str1, true);
xmlhttp.send();

}
</script>

<table cellpadding="1" cellspacing="1" border="0">
<tr>
<td id="txtHint">
<table cellpadding="1" cellspacing="1" border="0">
<tr>
<th style="padding-left:500px;">Serial Number</th>
<th>&nbsp;Status</th></tr>

<tr>

<td>
<?php $i = 1;
while ($i <= 10) {
echo $i++;  /* the printed value would be $i before the increment (post-increment) */
?>
<form>
<select name="process" id="process" onchange="showUser1(this.value)">
<option value="" selected="selected">Select</option>
<option value="0">Not Yet Start</option>
<option value="1">On Process...</option>
<option value="2">On Stock Yard</option>
<option value="3">Despatched</option>
</select>
<input type="text" name="snp" id="snp" value="<?php //echo $fetch['serial_number']; ?>" />
</form>
<?php } ?>
</td>
<td id="txtHint1"></td>
</tr>

</table>
</td>
</tr>
</table>

2 个答案:

答案 0 :(得分:0)

首先,我注意到你的代码中几乎没有错误。

  1. 不要在while循环中包含表单。

  2. 选择框名称和ID应该是动态的[如process1,process2 ..]

  3. 在函数showUser1()中,您必须传递两个参数。

  4.   

    onchange =“javascript:return showUser1([loop counter],this.value);”

    1. Javascript功能如下所示
    2.   

      function showUser1(cnt,processValue){

       var str = processValue;
      
       var str1 = document.getElementById("snp"+ cnt).value;
      
       ........   }
      
      1. 输入类型文本框值应该是动态的选择框。

答案 1 :(得分:0)

  

function showUser1(i,processValue){

     

var str = processValue; var str1 = document.getElementById(“snp”+   ⅰ)。价值;

     

警报(STR);警报(STR1);

     

if(str ==“”){document.getElementById(“txtHint1”)。innerHTML =   “”;返回; } if(window.XMLHttpRequest){// IE7 +的代码,   Firefox,Chrome,Opera,Safari xmlhttp = new XMLHttpRequest(); }   else {//代码为IE6,IE5 xmlhttp = new   的ActiveXObject( “Microsoft.XMLHTTP”); } xmlhttp.onreadystatechange =   function(){if(xmlhttp.readyState == 4&amp;&amp; xmlhttp.status == 200){             document.getElementById(“txtHint1”)。innerHTML =   xmlhttp.responseText; }} //   xmlhttp.open( “GET”, “?process.php Q =” + STR,TRUE); xmlhttp.open( “GET”,   “process.php?q =”+ str +“&amp; q1 =”+ str1,true); xmlhttp.send();

     

}

     

                                     序列号                     状态                 

          <tr>

              <td>
              <?php $i = 1;
              while ($i <= 10) {
                  ?>
                  <form>
                      <select name="process<?php echo $i;?>"
                          id="process<?php echo $i;?>"
                          onchange="showUser1(<?php echo $i;?>,this.value)">
                          <option value="" selected="selected">Select</option>
                          <option value="0">Not Yet Start</option>
                          <option value="1">On Process...</option>
                          <option value="2">On Stock Yard</option>
                          <option value="3">Despatched</option>
                      </select> <input type="text" name="snp<?php echo $i;?>"
                          id="snp<?php echo $i;?>"
                          value="<?php //echo $fetch['serial_number']; ?>" />
                  </form>



                   <?php  $i++;  /* the printed value would be $i before the increment (post-increment) */  }  ?> </td>
              <td id="txtHint1"></td>
          </tr>

      </table>        </td>   </tr> </table>