【问题标题】:Change radio button status when i click on any place of div当我单击 div 的任何位置时更改单选按钮状态
【发布时间】:2018-01-04 02:50:06
【问题描述】:
             EventPrices += "<div class='after-price' id=divPrice>";
                       if (localStorage.getItem('SelectedRoleName') != null)
                       {
                           if (localStorage.getItem('SelectedRoleName').toString().trim() == "Admin") {
                               EventPrices += "<span class='price'><input type='radio' value='" + Item.PriceId + "' id='chkEventPrices'  class='fa fa-check' name='chkEventPrices' title='" + Item.Price + "' onclick='CalculateTotal();'><label> $" + Item.Price + " </label> </span> ";
                           }
                           else {
                               EventPrices += "<span class='price'><input type='radio' value='" + Item.PriceId + "' id='chkEventPrices' disabled= 'disabled'  class='fa fa-check' name='chkEventPrices' title='" + Item.Price + "' onclick='CalculateTotal();'><label> $" + Item.Price + " </label> </span> ";
                           }
                       } else {
                           EventPrices += "<span class='price'><input type='radio' value='" + Item.PriceId + "' id='chkEventPrices' disabled= 'disabled'  class='fa fa-check' name='chkEventPrices' title='" + Item.Price + "' onclick='CalculateTotal();'><label> $" + Item.Price + " </label> </span> ";
                       }


                       EventPrices += "<span class='date'>After " + moment(Item.EarlyBirdDate).format("MMM DD, YYYY") + " </span>";
                       EventPrices += "</div></div>";
                       EventPrices += "<div><span id='spnEventPriceLiteralDetails_" + i + "'>" + EventPriceLiteraldetals + "</span></div>";


                    }


                    EventPrices += "</div><br/>";
                    i++;
                });

这是我的代码。现在,如果我单击它正在使用的特定单选按钮..我希望如果我单击框,则只有单选按钮将被选中。

【问题讨论】:

标签: jquery


【解决方案1】:

$('input[type=radio]').click(function() {
  $("#isClicked").text("selected radio value is " + ($('input[type=radio]:checked').val() == "1" ? "Male" : "Female"));
});
.hidden {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label for="r1"> Male
  <input type="radio" class="hidden" name="group1" id="r1" value="1" />
</label>
<label for="r2"> FeMale
  <input type="radio" class="hidden" name="group1" id="r2" value="2" />
</label>
<br />
<div id="isClicked"></div>

请检查这个。这里每当用户点击一个标签时,它对应的单选按钮点击事件就会被触发。

【讨论】:

    【解决方案2】:

    根据您 Q 猜测您的功能正在单击单选按钮。

    所以让我们假设你的radio 按钮类是.radio 并且'div' 类是.test 在这种情况下如果你愿意我们可以使用jQuery 的`.trigger'。 例如:

    $('body').on('click',`.test`,function(){
      $('.radio').trigger('click');
    });
    

    这将按照 me.ty 工作

    【讨论】:

    • 正文后缺少'
    猜你喜欢
    • 2013-08-04
    • 2010-10-20
    • 1970-01-01
    • 2016-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多