Onclick不能与bootstrap一起正常工作
onclick doesn't work properly with bootstrap
我尝试使用onclick事件与Bootstrap单选按钮来获取名称和属性,但它不太顺利。
有些单选按钮触发了事件,但没有得到名称或类("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>
jsfiddle.net/2sc8mc7L/6/
它不能工作,因为你以错误的方式混合了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>
相关文章:
- Javascript:selenium Web驱动程序isDisplayed()不工作
- jQuery UI自动完成突然停止工作
- AngularJS UI路由器不能像ng路由器那样工作
- HTML5音频加载和播放获胜'我不能在iPad上工作
- JavaScript打印功能使日历停止工作
- Javascript.getHours()工作不正常
- 为什么这在IE中的工作方式与在Firefox中不同
- 视频HTML没有'无法在Internet Explorer 11上工作
- 扩展移相器按钮类不工作
- Firebase迁移-简单的Firebase.set没有'不再工作了——旧的还是新的
- 谷歌地图不是以HTML显示,而是在JS Fiddle上工作
- 正在尝试使用if和else添加类,但无法正常工作
- Jquery FadeIn FadeOut 只工作一次
- Foreach无法在Typescript中工作
- 另一个ajax调用中的Jquery ajax调用在for循环中没有按预期工作
- 为什么不是't窗口.恢复正常工作吗?(javascript/jquery)
- JS可以在Chrome中工作,但不能在Firefox中工作
- 最大高度转换不;不工作,工作缓慢
- Gulp-rev:不是第一次工作,而是在第一次工作之后工作
- JQuery - Ajax: encodeUriComponent不工作(EncodeUri工作)