如果我单击选择按钮并在文本框中传递其值,如何获取所选单选按钮的值?

时间:2013-01-14 04:07:37

标签: php javascript

 //this is saved in president.php
if ($result = $mysqli->query("SELECT * FROM candidate_info WHERE position= 'president'"))
                    {
     // display records if there are records to display
       if ($result->num_rows > 0)
                            {
          // display records in a table
           echo "<table border='1' cellpadding='10'>";

              // set table headers

          echo "<tr><th>Student ID</td><th>Course</th><th>Name</th><th></th></tr>";

  while ($row = $result->fetch_object())
             {
        // set up a row for each record
   echo "<tr>";
    echo "<td>" . $row->studid . "</td>";
    echo "<td>" . $row->course . "</td>";
    echo "<td>" . $row->fname . " ". $row->mname ." ". $row->lname ."</td>";
echo "<td><input type ='radio' name='radio'  value='". $row->studid ."'> </td>";
    echo "</tr>";
                               }


  echo "<td> <input type='button' name='button' value='select' onclick='". get_radio_value() ."'></td>";

    echo "</table>";
     }

     else {
        echo "No results to display!";
                            }
      }

//这是我的javascript阅读并获取我的收音机的价值

   function get_radio_value()
      {
      for (var i=0; i < document.orderform.radio.length; i++)
        {
          if (document.orderform.radio[i].checked)
            {
             var rad_val = document.orderform.radio[i].value;
             }
        }
       }

//这里我会显示我选中的收音机的值,因为我点击了//文本框中我的表单内的选择按钮      “/&gt;

//但是当我加载我的页面时,我收到了错误

// like this Fatal error: Call to undefined function get_radio_value()

//这是我投票的代码.php

      <div id="TabbedPanels1" class="TabbedPanels">
      <ul class="TabbedPanelsTabGroup">
    <li class="TabbedPanelsTab" tabindex="0">President</li>
      <li class="TabbedPanelsTab" tabindex="0">Secretary</li>
      </ul>
    <div class="TabbedPanelsContentGroup">
      <div class="TabbedPanelsContent"><p>&nbsp;</p>
   <p><?php require_once('candidate/president.php'); ?></p>
     <p>&nbsp;</p></div>

              <td width="292" valign="top">
    <form method="post" action="voting.php" >
         <table width="289" height="76" border="0" cellspacing="1">

          <tr>
            <td width="131" height="24" ><div align="right">President</div></td>
            <td width="151"><div align="left">
                <input type="text" name="president" id="radio_value"/>
            </div></td>
          </tr>


        </table>
   </form></td>

1 个答案:

答案 0 :(得分:3)

更改此

echo "<td> <input type='button' name='button' value='select' onclick='". get_radio_value() ."'></td>";

在这里调用php的get_radio_value()函数,但函数是用javascript编写的,所以请使用下面的代码。

echo "<td> <input type='button' name='button' value='select' onclick='get_radio_value()'></td>";

用于传递文本框中单选按钮的值

<input type="text" name="president" value="WHAT CODE SHOULD I PUT HERE?" id="radio_value"/>

更改此行

var rad_val = document.orderform.radio[i].value;

return document.getElementById('radio_value').innerHTML = document.orderform.radio[i].value;