[英]Populate f.hidden_field with f.select value
I am looking at how to populate a hidden_field with the value of a f.select field however I am having difficulty. 我正在研究如何使用f.select字段的值填充hidden_field,但是我遇到了困难。 I was going to use javascript but the html created from my haml makes it difficult to assign an id to the select I wish to pass the value from. 我打算使用javascript,但是从haml创建的html使得很难将ID分配给希望从中传递值的选择。
= f.select :trial, [['Yes', 'true'], ['No', 'false']], {}, :id => "free-trial", class: 'selectpicker mandatory'
This generates the following html 这将生成以下html
<div class="col-sm-4">
<select class="selectpicker mandatory" id="free-trial" name="campaign[trial]" style="display: none;">
<option value="true">Yes</option>
<option value="false" selected="selected">No</option>
</select>
<div class="btn-group bootstrap-select mandatory">
<button type="button" class="btn dropdown-toggle" data-toggle="dropdown" data-id="free-trial" data-original-title="" title="">
<span class="filter-option pull-left">No</span>
<span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu" style="max-height: 133px; overflow-y: auto; min-height: 0px;">
<li rel="0">
<a tabindex="0" class="">
<span class="text">
Yes
</span>
<i class="icon-ok check-mark"></i>
</a>
</li>
<li rel="1" class="selected">
<a tabindex="0" class="">
<span class="text">No</span>
<i class="icon-ok check-mark"></i>
</a>
</li>
</ul>
</div>
</div>
This html when display is actually two selects one hidden with the ID assigned and a second which is displayed without the id associated. 当显示实际上是两个时,此html会选择一个隐藏的ID,并为其分配ID,然后隐藏第二个。 As a result my clicking on the visible select means the associated jquery doesnt fire. 结果,我单击可见的select表示关联的jquery不会触发。
If i understood you properly, you want catch value from select to hidden_input. 如果我正确理解您的要求,则希望将值从select捕获到hidden_input。 You can use jquery to do that: 您可以使用jquery来做到这一点:
$('#free-trial').on('change', function(){
var val = $(this).val();
$('#your_hidden_input').attr('value', val);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.