【问题标题】:Laravel Form:select() Multi select options not working if array value is numericLaravel Form:select() 如果数组值为数字,则多选选项不起作用
【发布时间】:2018-01-06 12:38:36
【问题描述】:

Form:select()

我想在选择中选择多个选项,但它只有在数组索引键是字符串时才有效,如果数组索引键是数字则不起作用...

请帮忙..

工作示例:

Form::select($field_data['name'], 
         array('L' => 'Large', 'M' => 'Medium', 'S' => 'Small'), //Options list
         array('S', 'M'), //Selected values
         array('multiple')); //Multiple True
//Result: Form print and Large and Small selected

不适用于数字数组键

Form::select($field_data['name'], 
         array('5' => 'Large', '2' => 'Medium', '10' => 'Small'),  //Options list
         array('10', '2'),  //Selected values
         array('multiple')); //Multiple True

// Just Form>select>options print, but no option selected

我想选择多个选项,选项键是数字ID..

【问题讨论】:

    标签: php forms laravel laravelcollective


    【解决方案1】:

    尝试将其更改为整数。

    Form::select($field_data['name'], 
             array(5 => 'Large', 2 => 'Medium', 10 => 'Small'),  //Options list
             array(10, 2),  //Selected values
             array('multiple')); //Multiple True
    

    【讨论】:

    • 谢谢...实际上我是动态的,所以我总是接受字符串值,现在我将更改我的代码,使其更灵活:)
    • @AsadRaza,欢迎。是的,如果可能的话,让一切变得灵活是一个很好的做法。 :)
    【解决方案2】:

    你可以这样尝试:

    $selected = array('10', '2'); //Selected values
    Form::select('sections[]',  ['5' => 'Large', '2' => 'Medium', '10' => 'Small'], $selected, ['multiple']);
    

    【讨论】:

      【解决方案3】:
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-14
      • 1970-01-01
      • 2016-10-06
      • 2015-08-07
      • 2012-12-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多