更改功能上的jQuery单选按钮运行两次

时间:2018-05-09 14:32:01

标签: javascript jquery

我的网站上有这个小功能:

$( 'form .radio_buttons' ).change( doTheFunction )

工作正常,但我注意到每当单选按钮选项更改时它总是运行两次。思考了5秒后,我意识到发生了什么;发生了2次变更事件:

change 1: the checked radio button is unchecked 
change 2: the unchecked radio button is checked

我的问题是如何将此视为一个事件?我可以使用一个灵活的Javascript函数吗?

编辑:
似乎这里可能存在更深层次的问题,我无法在我的应用程序之外复制问题,但Scott Marcus'答案对我有用。我必须弄清楚为什么改变事件会发射两次......

2 个答案:

答案 0 :(得分:1)

$('[name=myradio]').change(function(){
	 if($(this).is(':checked')){
	 	 alert('checked : '+ $(this).val());
	 }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>



<label><input type="radio" name="myradio" value="radio1">radio 1</label>
<label><input type="radio" name="myradio" value="radio2">radio 2</label>

答案 1 :(得分:1)

这真的不应该发生,如果没有你的代码我们真的无法确定问题是什么,但是你可以使用{{change事件来处理click事件。 1}}事件。

可能是您的应用程序中还有其他代码导致change回调被触发两次。

相关问题