Drupal 7 如何自动检索所选值

Drupal 7 如何自动检索所选值,drupal-7,drupal-modules,Drupal 7,Drupal Modules,是否有一种方法可以自动从下拉列表或单选列表中检索所选值,并将其发送到下面的表单? 例如,在下图中,如果我选择单选按钮“randy”,那么值“randy”将自动填入下面的表格Nama中 $header1 = array('Name'); // Prepare table header $query = db_select("homo", "r"); // Select table $query->fields("r", array("nama")); // Select fields

是否有一种方法可以自动从下拉列表或单选列表中检索所选值,并将其发送到下面的表单? 例如,在下图中,如果我选择单选按钮“randy”,那么值“randy”将自动填入下面的表格Nama中

$header1 = array('Name'); // Prepare table header
$query = db_select("homo", "r"); // Select table  
$query->fields("r", array("nama")); // Select fields    
$result = $query->execute(); // Execute query 
$rows = array();
while($data = $result->fetchObject()){ // Looping for filling the table rows  
$rows[] = array( // Fill the table rows
  $data->nama,
  );
}
$form['table'] = array (
 '#type' => 'tableselect',
 '#header' => $header1,
 '#options' => $rows,
 '#multiple' => FALSE,  
);
$form['f1'] = array(
 '#title' => t('Detail Anggota'),
 '#type' => 'fieldset',    
);
$form['f1']['namaa'] = array(
 '#title' => t('Nama'),
 '#type' => 'textfield',    
 '#required' => TRUE,
);


您可以使用javascript来完成这项工作。类似下面的代码可以轻松完成任务

<script type="text/javascript">
//<![CDATA[
    $(document).ready(function(){
         var checkbox = document.getElementsByName('checkbox_type');
         for(var i = 0; i < checkbox.length; i++) {
             if(checkbox[i].checked == true) {
                  $('input[name=namaa]').val($(checkbox[i]).text());
             }
         }
    });
//]]>
</script>

//

您还可以从钩子表单中添加JS,如下所示:

$form['#attached']['js'][] = drupal_get_path('module', 'mymodulename') . '/js/myjsname.js';

我应该把js脚本放在哪里?在模块下,还是在模块文件外?您必须在模块的模板文件内编写此javascript代码。