简体   繁体   English

默认情况下如何从数组设置多个选择选项

[英]How to set multiple select options by default from array

I'm trying to retrieve options/values from my database in the from of an array i would like to set these option/values as selected by default in a multiple select list and display them to the user where they will be able to updated their data if necessary. 我正在尝试从数据库的数组中检索选项/值,我想将这些选项/值设置为默认情况下在多个选择列表中选择的选项,并将其显示给用户,以便他们能够更新其必要的数据。

//data in database
$mytitle = array(
    'Arbitrator',
    'Attorney',
    'Student',
    'Other'
); 

//data for multiple select
$title = array(
    'Judge' ,
    'Magistrate' ,
    'Attorney' ,
    'Arbitrator',
    'Title Examiner' ,
    'Law Clerk','Paralegal' ,
    'Intern' ,
    'Legal Assistant',
    'Judicial Assistant',
    'Law Librarian' ,
    'Law Educator' ,
    'Attorney',
    'Student',
    'Other'
);

echo "<select name='title[]' multiple='multiple'>";

$test = implode(',', $mytitle);

for ($i=0; $i<=14; $i++) {
    if($test == $title[$i]) {
        echo "<option selected value='$title[$i]'>$title[$i]</option>";
    }
    else {
        echo "<option value='$title[$i]'>$title[$i]</option>";
    }
}

echo "</select>"; 

I think you may have a logic error. 我认为您可能有逻辑错误。 Try this as your loop: 尝试将此作为您的循环:

foreach ($title as $opt) {
    $sel = '';
    if (in_array($opt, $mytitle)) {
        $sel = ' selected="selected" ';
    }
    echo '<option ' . $sel . ' value="' . $opt . '">' . $opt . '</option>';
}

Very simple with the help of jQuery where the select has the id test 借助jQuery非常简单,其中select具有id测试

$('#test option').attr('selected', 'selected');

JSFiddle Example JSFiddle示例

Use the in_array() function. 使用in_array()函数。

for ($i=0; $i<=14; $i++) {
  if(in_array($title[$i], $mytitle)){
    echo "<option selected value='$title[$i]'>$title[$i]</option>";
  }else {
    echo "<option value='$title[$i]'>$title[$i]</option>";
  }
}
foreach ($title as &$x) 
{
echo "<option ".(in_array($x, $mytitle)?"selected":"")." value='$x'>$x</option>";
}

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

相关问题 如何使用PHP在选择框中选择从多个选项或具有不同值的数组到视图的设置选项 - How to set an option from multiple options or array with different values to views as selected in select box using PHP 如何根据php数组将预选选项设置为多选标记 - How to set preselected options into a multiple select tag according to a php array 如何选择里面的选项 <select multiple=“true”>从PHP数组? - How to select options inside <select multiple=“true”> from php array? 如何为 select 输入动态设置数组选项 - how to set array options dynamically for select input 默认情况下如何使用select2和Laravel选择一些多个选项 - How to select some multiple options by default using select2 and Laravel 如何将默认设置为选择2选择 - how to set default selected for select 2 multiple choice 设置多个选择列表选项以从查询中选择 - set multiple select list options to selected from query Yii:如何从数组中设置dropDownList&#39;options&#39; - Yii: How to set dropDownList 'options' from array 如何使用PHP数组中的值在HTML select控件中设置多个默认值? - How can I set multiple default values in an HTML select control using the values in a PHP array? jquery / php从jquery的php数组中选择多个选择的选项 - jquery / php select multiple selected options from php array in jquery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM