onclick 无法与 bootstrap 一起正常工作
onclick doesn't work properly with bootstrap
我尝试使用带有 Bootstrap 单选按钮的 onclick 事件来获取名称和属性,但有些地方并不顺利。
有些单选按钮触发了事件但没有得到名称或 class ("undefined"),有些则根本没有响应。
<div class="theContainer">
<input type="radio" class="myClass" name="options" id="option1" onclick="updateODid()" autocomplete="off" > Radio 1
<input type="radio" class="myClass" name="options" id="option2" onclick="updateODid()" autocomplete="off"> Radio 2
<input type="radio" class="myClass" name="options" id="option3" onclick="updateODid()" autocomplete="off"> Radio 3
<input type="hidden" class="findme" value="found me!">
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-primary active">
<input type="radio" class="myClass" name="options" id="option1" onclick="updateODid()" autocomplete="off" > Radio 1
</label>
<label class="btn btn-primary">
<input type="radio" class="myClass" name="options" id="option2" onclick="updateODid()" autocomplete="off"> Radio 2
</label>
<label class="btn btn-primary">
<input type="radio" class="myClass" name="options" id="option3" onclick="updateODid()" autocomplete="off"> Radio 3
</label>
</div>
</div>
它不起作用,因为您以错误的方式混合了 javascript 和 jquery。
请检查更新的代码段:https://jsfiddle.net/2sc8mc7L/12/
onclick 函数应如下所示:
$(".myClass").on('click',function(){
radioButtonClick($(this));
});
$("label.btn-primary").on('click',function(){
radioButtonClick($(this).find('.myClass'));
});
function radioButtonClick(_this){
alert("I'm in!")
alert (_this.attr('name'));
alert (_this.attr('class'));
alert (_this.closest(".theContainer").find(".findme").val());
}
您可以这样尝试,将 updateODid(this)
用于 onclick
,它将使用当前实例
function updateODid(button)
{
//alert("I'm in!")
alert ($(button).attr('name'));
alert ($(button).attr('class'));
alert ($(button).closest(".theContainer").find(".findme").val())
}
<div class="theContainer">
<input type="radio" class="myClass" name="options" id="option1" onclick="updateODid(this)" autocomplete="off" > Radio 1
<input type="radio" class="myClass" name="options" id="option2" onclick="updateODid(this)" autocomplete="off"> Radio 2
<input type="radio" class="myClass" name="options" id="option3" onclick="updateODid(this)" autocomplete="off"> Radio 3
<input type="hidden" class="findme" value="found me!">
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-primary active">
<input type="radio" class="myClass" name="options" id="option1" onclick="updateODid(this)" autocomplete="off" > Radio 1
</label>
<label class="btn btn-primary">
<input type="radio" class="myClass" name="options" id="option2" onclick="updateODid(this)" autocomplete="off"> Radio 2
</label>
<label class="btn btn-primary">
<input type="radio" class="myClass" name="options" id="option3" onclick="updateODid(this)" autocomplete="off"> Radio 3
</label>
</div>
</div><script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
我尝试使用带有 Bootstrap 单选按钮的 onclick 事件来获取名称和属性,但有些地方并不顺利。
有些单选按钮触发了事件但没有得到名称或 class ("undefined"),有些则根本没有响应。
<div class="theContainer">
<input type="radio" class="myClass" name="options" id="option1" onclick="updateODid()" autocomplete="off" > Radio 1
<input type="radio" class="myClass" name="options" id="option2" onclick="updateODid()" autocomplete="off"> Radio 2
<input type="radio" class="myClass" name="options" id="option3" onclick="updateODid()" autocomplete="off"> Radio 3
<input type="hidden" class="findme" value="found me!">
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-primary active">
<input type="radio" class="myClass" name="options" id="option1" onclick="updateODid()" autocomplete="off" > Radio 1
</label>
<label class="btn btn-primary">
<input type="radio" class="myClass" name="options" id="option2" onclick="updateODid()" autocomplete="off"> Radio 2
</label>
<label class="btn btn-primary">
<input type="radio" class="myClass" name="options" id="option3" onclick="updateODid()" autocomplete="off"> Radio 3
</label>
</div>
</div>
它不起作用,因为您以错误的方式混合了 javascript 和 jquery。
请检查更新的代码段:https://jsfiddle.net/2sc8mc7L/12/ onclick 函数应如下所示:
$(".myClass").on('click',function(){
radioButtonClick($(this));
});
$("label.btn-primary").on('click',function(){
radioButtonClick($(this).find('.myClass'));
});
function radioButtonClick(_this){
alert("I'm in!")
alert (_this.attr('name'));
alert (_this.attr('class'));
alert (_this.closest(".theContainer").find(".findme").val());
}
您可以这样尝试,将 updateODid(this)
用于 onclick
,它将使用当前实例
function updateODid(button)
{
//alert("I'm in!")
alert ($(button).attr('name'));
alert ($(button).attr('class'));
alert ($(button).closest(".theContainer").find(".findme").val())
}
<div class="theContainer">
<input type="radio" class="myClass" name="options" id="option1" onclick="updateODid(this)" autocomplete="off" > Radio 1
<input type="radio" class="myClass" name="options" id="option2" onclick="updateODid(this)" autocomplete="off"> Radio 2
<input type="radio" class="myClass" name="options" id="option3" onclick="updateODid(this)" autocomplete="off"> Radio 3
<input type="hidden" class="findme" value="found me!">
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-primary active">
<input type="radio" class="myClass" name="options" id="option1" onclick="updateODid(this)" autocomplete="off" > Radio 1
</label>
<label class="btn btn-primary">
<input type="radio" class="myClass" name="options" id="option2" onclick="updateODid(this)" autocomplete="off"> Radio 2
</label>
<label class="btn btn-primary">
<input type="radio" class="myClass" name="options" id="option3" onclick="updateODid(this)" autocomplete="off"> Radio 3
</label>
</div>
</div><script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>