簡體   English   中英

如果我單擊選擇按鈕並在文本框中傳遞其值,如何獲得所選單選按鈕的值?

[英]how can i get the value of selected radio button if i click the select button and pass its value in a textbox?

 //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;
             }
        }
       }

//在這里,我將在單擊//文本框(位於表單“ />”中)上的“選擇”按鈕時顯示所選收音機的值

//但是當我加載頁面時,出現錯誤

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

//這是我的voting.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>

改變這個

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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM