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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀