[英]jQuery returning incorrect value of checked radio button when using styled buttons
I'm using the jQuery plugin ScrewDefaultButtons to style my radio buttons, but it seems it comes with a side effect that it doesn't return the correct value when getting it via jQuery. 我正在使用jQuery插件ScrewDefaultButtons设置单选按钮的样式,但似乎有一个副作用,即通过jQuery获取时,它不会返回正确的值。 I am running a function when they are clicked and I am trying to get the value of the radio button the user selected, but it seems to returns the value of the previously selected radio button instead of the one that was just selected.
单击它们时,我正在运行一个函数,并且尝试获取用户选择的单选按钮的值,但是它似乎返回先前选择的单选按钮的值,而不是刚刚选择的那个。
If I remove the class pretty_rb
, which disables the styling then it works fine. 如果我删除了
pretty_rb
类,它将禁用样式设置,那么它将正常工作。
I am trying to get the value with: 我试图通过以下方式获得价值:
jQuery('input[name=tax_account_type]:checked').val();
My HTML: 我的HTML:
<input type="radio" name="tax_account_type" id="individual" class="pretty_rb" value="0" onclick="updateRegForm();" checked required>
<input type="radio" name="tax_account_type" id="business" class="pretty_rb" value="1" onclick="updateRegForm();" required>
Edit: Fiddle: http://jsfiddle.net/94UtB/2/ 编辑:小提琴: http : //jsfiddle.net/94UtB/2/
The problem is that ScrewDefaultButtons
wraps the input
elements in a div
and then hides the input
element. 问题在于
ScrewDefaultButtons
将input
元素包装在div
,然后隐藏了input
元素。
This div has a click event that checks the correct hidden input
element. 该div有一个click事件,用于检查正确的隐藏
input
元素。
However. 然而。 Since you have an
onClick
event YOUR event is run before the change. 由于您有
onClick
事件,因此您的事件是在更改之前运行的。
That is why you get the old
value. 这就是为什么您获得
old
价值。
To solve this add the following code after the call to screwDefaultButtons
: 为了解决这个问题,在调用
screwDefaultButtons
之后添加以下代码:
$('.styledRadio').on('click', updateRegForm);
or 要么
$('.pretty_rb').on('click', updateRegForm);
Remember to remove the onlick attribute from the HTML. 记住要从HTML中删除onlick属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.