如何从javascript中的选择框中获取价值?

时间:2012-01-07 12:07:31

标签: php javascript jquery html ajax

                                <table border="1">
                                    <tr>
                                        <th>
                                            SR No.<br />
                                        </th>
                                        <th>
                                            Name
                                        </th>
                                        <th>
                                            Address
                                        </th>
                                        <th>
                                            Area
                                        </th>
                                        <th>
                                            City
                                        </th>
                                        <th>
                                            Contact No.
                                        </th>
                                        <th>
                                            Volunteer List
                                        </th>
                                        <th>
                                            Bird Namea
                                        </th>
                                        <th>
                                            Status
                                        </th>
                                        <th>
                                            Send SMS
                                        </th>
                                    </tr>
                                    <?php
                                        while($row = mysql_fetch_array($pager->result))
                                        {
                                    ?>
                                    <tr>
                                        <td>
                                            <?php echo $row['InqID']; ?>
                                        </td>
                                        <td>
                                            <?php echo $row['InqFromName']; ?>
                                        </td>
                                        <td>
                                            <?php echo $row['InqFromAddress']; ?>
                                        </td>
                                        <td>
                                            <?php echo $row['InqArea']; ?>
                                        </td>
                                        <td>
                                            <?php echo $row['InqCity']; ?>
                                        </td>
                                        <td>
                                            <?php echo $row['InqContactNo']; ?>
                                        </td>
                                        <td>
                                        <select name="volunteerSelect1" id="volunteerSelect1">
                                            <option value="" selected="selected">Please Select Volunteer</option>
                                            <?php
                                                $select="SELECT * FROM tran_ngovolent where ngovolentNGOCode=".$ngoSelectId;
                                                //echo $select;
                                                $result=mysql_query($select);
                                                while($rowBird=mysql_fetch_array($result))
                                                {
                                                    echo"<option value=".$rowBird['ngovolentMobile']." id='volmobile'>".$rowBird['ngovolentName']."</option>";
                                                }
                                            ?>
                                        </select>
                                        </td>
                                        <td>
                                            <?php 
                                                $birdID=$row['InqBirdType'];
                                                $birdSelect="SELECT Bird_Name FROM mst_bird WHERE Bird_Code=".$birdID;
                                                $result=mysql_query($birdSelect);
                                                while($bird_name=mysql_fetch_array($result))
                                                {
                                                    $birdname=$bird_name['Bird_Name'];
                                                    echo $birdname;
                                                }
                                            ?>
                                        </td>
                                        <td>
                                            <?php
                                                $inqStatus=$row['InqStatus'];
                                                if($inqStatus==0)
                                                {
                                                    echo "<a href='index.php?id=".$row['InqID']."&status=status&page=".$pageid."' onclick='return confirmAction()'>Pending</a>";

                                                }else
                                                {
                                                    echo "Finished";
                                                }
                                            ?>
                                        </td>
                                        <td>
                                            <input type="button" name="SendSMS" value="Send SMS" onclick="smsNgo(<?php echo $birdID;?>,<?php echo "'".$row['InqFromAddress']."'";?>,<?php echo "'".$birdname."'";?>,<?php echo "'".$row['InqCity']."'";?>,<?php echo "'".$row['InqArea']."'";?>,<?php echo "'".$row['InqFromName']."'";?>,<?php echo "'".$row['InqContactNo']."'";?>);return sms()"/> 
                                        </td>
                                    </tr>
                                    <?php
                                        }
                                    ?>
                                    <tr>
                                        <td colspan="10">
                                           <?php echo $pager->show(); ?>
                                        </td>
                                    </tr>      
                                </table>

在本程序中,我从我的选择框中的数据库中获取值。显示这个图像 enter image description here

在第一个选择框中,我在我的JavaScript中得到了完美的结果,但是其他一个选择框值为null或为空这是我的JavaScript和Ajax代码。

function smsNgo(birdno,address,birdname,city,area,name,contact)
{
//document.write(mobile1);  
var ngocontact = document.getElementById("volunteerSelect1").value;
//var ngocontact = selectElement.options[selectElement.selectedIndex].value;

var xmlhttp;
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("smsStatus").innerHTML=xmlhttp.responseText;; 
    window.open("http://localhost/goal_bird/admin/edit.php","_self");

   }
  }
 var url="http://www.****.co.in/sendsms.aspx?mobile=******&pass=******&    senderid=SMSIdea&to="+ngocontact+"&msg=Bird id is "+birdno+" "+birdname+" "+name+"     "+address+" "+area+" "+city+" "+contact;
document.write(url);
xmlhttp.open("POST",url,true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send();
setTimeout("location.href='index.php'",2000);
}

4 个答案:

答案 0 :(得分:1)

您可以通过添加一些db unique id来使volunteerSelect1名称唯一。在这种情况下可能是InqID

<select name="volunteerSelect_<?php echo $row['InqID']; ?>" 
    id="volunteerSelect_<?php echo $row['InqID']; ?>">

答案 1 :(得分:0)

从一眼望去,我可以看到所有<select>标签的名称=“”是相同的......“志愿者选择1”

尝试使用志愿者选择[id]或志愿者选择[]

相同的东西适用于id =“”属性,它们都使用相同的ID,javascript使用它来识别特定的DOM项目

但是你不能在id中使用[]尝试志愿者选择_ID

这可能就是问题,ID属性

答案 2 :(得分:0)

将此<select name="volunteerSelect1" id="volunteerSelect1">修改为此

<select name="volunteerSelect1" id="volunteerSelect<?php echo $row['InqID']; ?>">

然后这个<input type="button" name="SendSMS" value="Send SMS" onclick="smsNgo(<?php echo $birdID;?>,<?php echo "'".$row['InqFromAddress']."'";?>,<?php echo "'".$birdname."'";?>,<?php echo "'".$row['InqCity']."'";?>,<?php echo "'".$row['InqArea']."'";?>,<?php echo "'".$row['InqFromName']."'";?>,<?php echo "'".$row['InqContactNo']."'";?>);return sms()"/>

对此 <input type="button" name="SendSMS" value="Send SMS" onclick="smsNgo(<?php echo $row['InqID']; ?>,<?php echo $birdID;?>,<?php echo "'".$row['InqFromAddress']."'";?>,<?php echo "'".$birdname."'";?>,<?php echo "'".$row['InqCity']."'";?>,<?php echo "'".$row['InqArea']."'";?>,<?php echo "'".$row['InqFromName']."'";?>,<?php echo "'".$row['InqContactNo']."'";?>);return sms()"/>

然后这个function smsNgo(birdno,address,birdname,city,area,name,contact) 这个function smsNgo(inqid,birdno,address,birdname,city,area,name,contact)

最后是var ngocontact = document.getElementById("volunteerSelect1").value;

到此var ngocontact = document.getElementById("volunteerSelect" + inqid).value;

基本上,我做了什么,我根据inqid为每个选择框添加了一个唯一的标识符。然后我用这个ID调用函数smsNgo并选择正确的选择框:)

答案 3 :(得分:0)

因为您将此标记为jquery。因此,要从选择下拉列表中获取值,您将使用类似this

的内容