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

图片转代码服务由CSDN问答提供 功能建议

我有这段代码:

 &lt; div class =  “form-group {{(isset($ errors)AND $ errors-&gt; has('indicacao_interna'))?'has-error':''}}”&gt; 
 {{Form :: label('indicacao_interna  ','IndicaçãoInterna',数组('class'=&gt;'col-lg-4 control-label'))}} 
&lt; div class =“col-lg-3”&gt; 
&lt; div&gt;  ; 
 
&lt; label class ='radio-inline'&gt; {{Form :: radio('indicacao_interna',1,oldRadio('indicacao_interna',1,true))}} Sim&lt; / label&gt; 
  &lt; label class ='radio-inline'&gt; {{Form :: radio('indicacao_interna',0,oldRadio('indicacao_interna',0,true))}}Não&lt; / label&gt; 
 @include('partials  .validators.message_field',array('field'=&gt;'indicacao_interna'))
&lt; / div&gt; 
&lt; / div&gt; 
&lt; / div&gt; 
   \  n 
 

它生成此代码:

 &lt; div class =“form-group”&gt; 
&lt; label for =“indicacao_interna”class =  “col-lg-4 control-label”&gt;IndicaçãoInterna&lt; / label&gt;  &lt; div class =“col-lg-3”&gt; 
&lt; div&gt; 
 
&lt; label class =“radio-inline”&gt;&lt; input checked =“checked”name =“indicacao_interna”type  =“radio”value =“1”id =“indicacao_interna”&gt;  Sim&lt; / label&gt; 
&lt; label class =“radio-inline”&gt;&lt; input name =“indicacao_interna”type =“radio”value =“0”id =“indicacao_interna”&gt;  Não&lt; / label&gt; 
&lt; / div&gt; 
&lt; / div&gt; 
   
 
 

如您所见,单选按钮具有相同的ID 即可。 这是错的,对吗?

并且......为什么收音机会生成相同的ID? 不只是一个名字?


  • Laravel 4.1
  • Apache 2.4
  • PHP 5.4.17

    对不起我的英文...谢谢 < / DIV>

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

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')) }}
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dstk51319 2014-06-17 15:00

    Tenta assim:

    {{ Form::radio('indicacao_interna', 1, oldRadio('indicacao_interna', 1, true),
        array('id' => 'indicacao_interna_1')) }}
    
    {{ Form::radio('indicacao_interna', 1, oldRadio('indicacao_interna', 1, true), 
        array('id' => 'indicacao_interna_2')) }}
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题