dqnhfbc3738 2014-06-17 14:46
浏览 101
已采纳

Laravel 4.1.x中具有相同ID的单选按钮

I have this code:

<div class="form-group  {{ (isset($errors) AND $errors->has('indicacao_interna')) ? 'has-error' : '' }}">
  {{ Form::label('indicacao_interna', 'Indicação Interna', array('class' => 'col-lg-4 control-label')) }}
  <div class="col-lg-3">
    <div>

      <label class='radio-inline'>{{ Form::radio('indicacao_interna', 1, oldRadio('indicacao_interna', 1, true)) }} Sim</label>
      <label class='radio-inline'>{{ Form::radio('indicacao_interna', 0, oldRadio('indicacao_interna', 0, true)) }} Não</label>
      @include('partials.validators.message_field', array('field' => 'indicacao_interna'))
    </div>
  </div>
</div>

And it generates this code:

<div class="form-group  ">
  <label for="indicacao_interna" class="col-lg-4 control-label">Indicação Interna</label>                <div class="col-lg-3">
  <div>

    <label class="radio-inline"><input checked="checked" name="indicacao_interna" type="radio" value="1" id="indicacao_interna"> Sim</label>
    <label class="radio-inline"><input name="indicacao_interna" type="radio" value="0" id="indicacao_interna"> Não</label>
  </div>
</div>

As you can see, the radio button have the same id. This is wrong, right?

And... why the radio generate same id? Not just a name?


  • Laravel 4.1
  • Apache 2.4
  • PHP 5.4.17

Sorry for my english... thanks

  • 写回答

2条回答 默认 最新

  • doubeng1278 2014-06-17 15:13
    关注

    Yeah, that's a small drawkback of automatic code creation. Behind the scenes, Form::radio() calls the same $this->input($type, $name, $value = null, $options = array()) general function for the inputs, which inside calls:

    $id = $this->getIdAttribute($name, $options);
    

    The method getIdAttribute() has this definition:

    public function getIdAttribute($name, $attributes)
    {
        if (array_key_exists('id', $attributes))
        {
            return $attributes['id'];
        }
    
        if (in_array($name, $this->labels))
        {
            return $name;
        }
    }
    

    That means, if it finds a specified ID in the options provided it returns that, else it returns the name.

    Which is a comfortable and perfectly valid solution for the other input types, but doesn't work well for radios and checkboxes, where you need to provide your own id.

    {{ Form::radio('indicacao_interna', 1, oldRadio('indicacao_interna', 1, true),
        array('id' => 'custom_id')) }}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式