0

我正在处理一个包含许多下拉框的表单,并且我已经从数据库中检索了下拉列表的值。一切都很完美,但现在我想添加一个提交按钮。在提交表单时,我想检索选定的下拉值及其 id。我不知道该怎么做。请帮助我。

提前致谢。

这是我的代码-

      <!DOCTYPE html>
      <html>
      <?php
      $db = JFactory::getDBO();



      ?>
      <head>
      <script>
          var showClasses = {

          };
          function fnchecked(blnchecked,className)
          {
              if(blnchecked)
              {
                  showClasses[className] = true;
              }
              else
              {
                  showClasses[className] = false;
              }
              var ps = document.getElementById("div-block").children;
              var psNumber = ps.length;
              for (var i = 0; i < psNumber; i ++) {
                  var have = hasClasses(ps[i], showClasses);
                  if (have) {
                      ps[i].style.display = "";
                  } else {
                      ps[i].style.display = "none";
                  }
              }
          }

          function hasClasses(element) {
              var result = false;
              for (var cls in showClasses) {
                  if (showClasses[cls]) {
                      if ((' ' + element.className + ' ').indexOf(' ' + cls + ' ') > -1) {
                          result = true;
                      }
                  }
              }
              return result
          }
      </script>
      </head>
      <body>

      <form name="shohide1" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
      <div>
          <label>Technologies: </label></br>
          <input type="checkbox" name="technologies1" onChange="fnchecked(this.checked,'question1');" /> Kingspan Solar</br>
          <input type="checkbox" name="technologies2" onChange="fnchecked(this.checked,'question2');" /> Solar PVT</br>
          <input type="checkbox" name="technologies3" onChange="fnchecked(this.checked,'question3');" /> Insulation</br>
          <input type="checkbox" name="technologies4" onChange="fnchecked(this.checked,'question4');" /> Gas boiler</br>
          <input type="checkbox" name="technologies5" onChange="fnchecked(this.checked,'question5');" /> Oil boiler</br>
          <input type="checkbox" name="technologies6" onChange="fnchecked(this.checked,'question6');" /> Heat recovery ventilation</br>
          <input type="submit" name="formSubmit" value="Get The Info">


          <div id="div-block" >
              <p class=" question1 " style="display: none">

              <?php
              $query = $db->getQuery(true);
              $query = 'select a.question, b.answer from #__questions As a RIGHT JOIN #__answers As b ON a.id = b.question_id where b.question_id=1';
              $db->setQuery((string)$query);
              $results = $db->loadObjectList();
              if ($results)
              {
                  print_r($results['0']->question);
                  echo '<select>';
                  echo '<option value="">Click & Choose</option>';
                  foreach($results as $result) 
                  {
                      echo '<option value="'.$result->answer.'">'.$result->answer.'</option>';
                  }
                  echo '</select>';
              }
              ?>
              </p>
              <p class="question1 question2 question3 question4 question5 question6 " style="display: none">
              <?php
                $query = $db->getQuery(true);
                $query = 'select a.question, b.answer from #__questions As a RIGHT JOIN #__answers As b ON a.id = b.question_id where b.question_id=2';
                $db->setQuery((string)$query);
                $results = $db->loadObjectList();
                if ($results)
                {
                    print_r($results['0']->question);
                    echo '<select>';
                    echo '<option value="">Click & Choose</option>';
                    foreach($results as $result) 
                    {
                        echo '<option value="'.$result->answer.'">'.$result->answer.'</option>';
                    }
                    echo '</select>';
                }
              ?>
              </p>
              <p class="question1 question2  " style="display: none">
               <?php
                $query = $db->getQuery(true);
                $query = 'select a.question, b.answer from #__questions As a RIGHT JOIN #__answers As b ON a.id = b.question_id where b.question_id=3';
                $db->setQuery((string)$query);
                $results = $db->loadObjectList();
                if ($results)
                {
                    print_r($results['0']->question);
                    echo '<select>';
                    echo '<option value="">Click & Choose</option>';
                    foreach($results as $result) 
                    {
                        echo '<option value="'.$result->answer.'">'.$result->answer.'</option>';
                    }
                    echo '</select>';
                }
              ?>
              </p>
              <p class="question1 question2 question3 question4 question5 " style="display: none">
              <?php
                $query = $db->getQuery(true);
                $query = 'select a.question, b.answer from #__questions As a RIGHT JOIN #__answers As b ON a.id = b.question_id where b.question_id=4';
                $db->setQuery((string)$query);
                $results = $db->loadObjectList();
                if ($results)
                {
                    print_r($results['0']->question);
                    echo '<select>';
                    echo '<option value="">Click & Choose</option>';
                    foreach($results as $result) 
                    {
                        echo '<option value="'.$result->answer.'">'.$result->answer.'</option>';
                    }
                    echo '</select>';
                }
              ?>
              </p>
              <p class=" question2 " style="display: none">
              <?php
                $query = $db->getQuery(true);
                $query = 'select a.question, b.answer from #__questions As a RIGHT JOIN #__answers As b ON a.id = b.question_id where b.question_id=5';
                $db->setQuery((string)$query);
                $results = $db->loadObjectList();
                if ($results)
                {
                    print_r($results['0']->question);
                    echo '<select>';
                    echo '<option value="">Click & Choose</option>';
                    foreach($results as $result) 
                    {
                        echo '<option value="'.$result->answer.'">'.$result->answer.'</option>';
                    }
                    echo '</select>';
                }
              ?>
             </p>
             <p class=" question3 " style="display: none">
             <?php
              $query = $db->getQuery(true);
              $query = 'select a.question, b.answer from #__questions As a RIGHT JOIN #__answers As b ON a.id = b.question_id where b.question_id=6';
              $db->setQuery((string)$query);
              $results = $db->loadObjectList();
              if ($results)
              {
                  print_r($results['0']->question);
                  echo '<select>';
                  echo '<option value="">Click & Choose</option>';
                  foreach($results as $result) 
                  {
                      echo '<option value="'.$result->answer.'">'.$result->answer.'</option>';
                  }
                  echo '</select>';
              }
              ?>
             </p>
              <p class=" question3 " style="display: none">
              <?php
                $query = $db->getQuery(true);
                $query = 'select a.question, b.answer from #__questions As a RIGHT JOIN #__answers As b ON a.id = b.question_id where b.question_id=7';
                $db->setQuery((string)$query);
                $results = $db->loadObjectList();
                if ($results)
                {
                    print_r($results['0']->question);
                    echo '<select>';
                    echo '<option value="">Click & Choose</option>';
                    foreach($results as $result) 
                    {
                        echo '<option value="'.$result->answer.'">'.$result->answer.'</option>';
                    }
                    echo '</select>';
                }
              ?>
              </p>
              <p class=" question4 question5 question6 " style="display: none">
              <?php
                $query = $db->getQuery(true);
                $query = 'select a.question, b.answer from #__questions As a RIGHT JOIN #__answers As b ON a.id = b.question_id where b.question_id=8';
                $db->setQuery((string)$query);
                $results = $db->loadObjectList();
                if ($results)
                {
                    print_r($results['0']->question);
                    echo '<select>';
                    echo '<option value="">Click & Choose</option>';
                    foreach($results as $result) 
                    {
                        echo '<option value="'.$result->answer.'">'.$result->answer.'</option>';
                    }
                    echo '</select>';
                }
              ?>
              </p>
              <p  class=" question4 question5 " style="display: none">
              <?php
                $query = $db->getQuery(true);
                $query = 'select a.question, b.answer from #__questions As a RIGHT JOIN #__answers As b ON a.id = b.question_id where b.question_id=9';
                $db->setQuery((string)$query);
                $results = $db->loadObjectList();
                if ($results)
                {
                    print_r($results['0']->question);
                    echo '<select>';
                    echo '<option value="">Click & Choose</option>';
                    foreach($results as $result) 
                    {
                        echo '<option value="'.$result->answer.'">'.$result->answer.'</option>';
                    }
                    echo '</select>';
                }
              ?>
             </p>
             <p  class=" question4 question5 " style="display: none">
             <?php
              $query = $db->getQuery(true);
              $query = 'select a.question, b.answer from #__questions As a RIGHT JOIN #__answers As b ON a.id = b.question_id where b.question_id=10';
              $db->setQuery((string)$query);
              $results = $db->loadObjectList();
              if ($results)
              {
                  print_r($results['0']->question);
                  echo '<select>';
                  echo '<option value="">Click & Choose</option>';
                  foreach($results as $result) 
                  {
                      echo '<option value="'.$result->answer.'">'.$result->answer.'</option>';
                  }
                  echo '</select>';
              }
             ?>
             </p>
             <p  class=" question6 " style="display: none">
             <?php
              $query = $db->getQuery(true);
              $query = 'select a.question, b.answer from #__questions As a RIGHT JOIN #__answers As b ON a.id = b.question_id where b.question_id=11';
              $db->setQuery((string)$query);
              $results = $db->loadObjectList();
              if ($results)
              {
                  print_r($results['0']->question);
                  echo '<select>';
                  echo '<option value="">Click & Choose</option>';
                  foreach($results as $result) 
                  {
                      echo '<option value="'.$result->answer.'">'.$result->answer.'</option>';
                  }
                  echo '</select>';
              }
              ?>
            </p>
          </div>
      </div>


      </body>
      </html>
4

2 回答 2

0

您可以使用隐藏类型获取值。

为了您更好地理解,我添加了一种隐藏类型和一个新按钮。您可以在 onselect 下拉事件中看到值。这可能对您没什么帮助:

<script>
function setSelectedValue() {

    var objSelect = document.getElementById("demo_onselect").value;
    //alert(objSelect); //your selected value
     document.getElementById('newbutton').value = objSelect; //assign your selected value to new button
      document.getElementById('hiddenfield').value = objSelect; //assign your selected value to new button
     var hiddenid = document.getElementById('newbutton').value;
      var newbtnval = document.getElementById('newbutton').value;
     alert("hidden field value is: "+hiddenid);
      alert("new button  value is: "+newbtnval);

}



</script>
<select style="width: 280px" id="demo_onselect" name="demo_onselect" onchange="setSelectedValue();">
  <option selected="">Please Select</option>
  <option>1</option>
  <option>2</option>
  <option>3</option>

</select>
<input type="hidden" id="hiddenfield" name="hiddenfield" value="" />
<input type="button" id="newbutton" name="newbutton" value="" />

现场演示:>>

于 2013-05-09T08:50:29.157 回答
0

尝试这个

选择 id="第一"

选项 onclick="getValue('.$result->answer.')">

函数 getValue(val){

$('#first').attr("name","val");

}

于 2013-05-09T08:38:40.880 回答