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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器