如何使用单选按钮选中或单击属性在我的情况下

How to use radio button checked or clicked property in my case

本文关键字:属性 我的 情况下 单击 何使用 单选按钮      更新时间:2023-09-26

大家好,我正在使用Rails 3.2.13。我在我的视图文件中使用了下面的脚本。如果有人点击ownertype Other,div与id=group_user_id将打开,最后我将保存这些操作。到目前为止,当我使用f.r radio_button时,它是好的。

<div class="form-group">
  Owner:
  <%= f.radio_button :owner_type,"Self", :id=>"self-group", :checked =>true %>
  Self
  <%= f.radio_button :owner_type,"Other",:id=>"other-group" %>
  Others
</div>
<div id="group_user_id" class="form-group">
  <span>
        <%= text_field_tag "account_id", nil, :id => "autocomplete_text", :class => "form-control", :placeholder => "Account Id" %>
      </span> 
</div> 
Javascript代码

<script>
$(document).ready(function() {
 $("#group_user_id").hide();
 $("#self-group").prop("checked", true);
$("#other-group").click(function() {
  $("#group_user_id").show();
});

});

现在我使用模板css单选按钮,之后它不工作。以下是代码供参考。

在这种情况下,单选按钮类使用正方形绿色单行。而Rails的单选按钮是隐藏的,虽然我已经提供了f.r radio_button并传递了适当的id。

<div class="row">
    <div class="col-sm-2">
      <div class="text-green" style="margin-top: 9px; font-size: 18px;">
        Owner
      </div>
    </div>
    <div class="col-sm-2">
      <div class="icheck">
        <div class="square-green single-row">
          <div class="radio">
            <%= f.radio_button :owner_type,"Self", :id=>"self-group", :checked =>true %>
            <label>Self</label>
          </div>
        </div>
      </div>
    </div>
    <div class="col-sm-2">
      <div class="icheck">
        <div class="square-green single-row">
          <div class="radio">
            <%= f.radio_button :owner_type,"Other",:id=>"other-group" %>
            <label>Others </label>
          </div>
        </div>
      </div>
    </div>
  </div>

我使用了以下js和css。

<%= stylesheet_link_tag "css/iCheck/skins/square/green.css" %>
<%= javascript_include_tag "js/iCheck/jquery.icheck.js", "js/icheck-init.js" %>

在这种情况下,当我们使用单选按钮从模板,如何管理单选按钮点击,检查和其他属性。

据我所知,您需要在radio_button_tag....中输入类名

radio_button_tag应该有如下格式:

radio_button_tag(name, value, checked = false, options = {})

试试这个:

<%= f.radio_button :owner_type,"Self", :id=>"self-group", :checked =>true, :class=> 'class-name' %>

允许class属性

:checked=>true表示默认选中单选按钮

我使用了ruby gem和一些javascript逻辑来隐藏和显示div。这个脚本对我很有帮助。

在Gemfile中放置代码。

gem 'icheck-rails'
在这个gem的帮助下,我们可以很容易地为单选按钮或复选框提供类,如下所示:
<%= f.radio_button :owner_type, "Self", :id=>"self-group", :class => 'icheck-me', "data-skin"=>'square', "data-color"=>'green' %>

Javascript逻辑
<script>
   $(document).ready(function () {
   $('input').on('ifClicked', function (event) {
     var value = $(this).val();
     if (value == "Other") {
       $("#group_user_id").show(); 
      }
      else if (value == "Self") {
        $("#group_user_id").hide();
      }
    });
 });
</script>