简体   繁体   English

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

[英]How can i get value from selection box in javascript?

                                <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>

In this Program i get value from database in my selection box. 在此程序中,我从选择框中从数据库中获取价值。 show this image 显示此图片 在此处输入图片说明

In first selection box i got perfect out put in my JavaScript but other one selection box value is null or empty Here is my JavaScript and Ajax code. 在第一个选择框中,我将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);
}

You can make the volunteerSelect1 name unique by adding some db unique id with it. 您可以通过添加一些db唯一ID来使志愿者Select1名称唯一。 May be InqID in this case? 在这种情况下可能是InqID

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

From a glance, i can see the name="" for all the <select> tags are the same... "volunteerSelect1" 乍一看,我可以看到name =“”表示所有<select>标记都相同...“ volunteerSelect1”

try using something like volunteerSelect[id] or volunteerSelect[] 尝试使用类似自愿者选择[id]或自愿者选择[]的方法

The SAME thing applies to the id="" attribute, they are all using the same ID, this is used by javascript to identify a particular DOM item 同样的事情适用于id =“”属性,它们都使用相同的ID,javascript使用它来标识特定的DOM项目

However you cant use [] in the id try volunteerSelect_ID 但是,您不能在ID中使用[]尝试使用自愿选择者ID

That's probably the problem, the ID attribute 可能就是这个问题,ID属性

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

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

then this <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 $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()"/>

to this <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()"/> 对此<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()"/>

then this function smsNgo(birdno,address,birdname,city,area,name,contact) to this function smsNgo(inqid,birdno,address,birdname,city,area,name,contact) 然后此function smsNgo(birdno,address,birdname,city,area,name,contact)到此function smsNgo(inqid,birdno,address,birdname,city,area,name,contact)

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

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

Basically, what I did that i added an unique identifier to each selectbox according to inqid. 基本上,我所做的就是根据inqid在每个选择框中添加了一个唯一的标识符。 Then I call function smsNgo with this ID and select the right selectbox :) 然后我使用此ID调用函数smsNgo并选择正确的选择框:)

Since you tagged this as jquery. 由于您将其标记为jquery。 so to get the value from select dropdown, you will use something like this 因此要获得从下拉列表中选择的价值,你会使用类似这样

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM