如何使用 jquery 触发单选按钮的初始更改事件?
How do I trigger an initial change event for radio buttons with jquery?
我正在尝试 enable/disable 一组单选按钮底部的 "other" 字段,就像这样...
window.init = function() {
debugger;
var form = $("#myform");
var enableOther = function() {
var other = $(this).val() == 'other';
form.find('input[name=other]').prop('disabled', !other);
};
var options = form.find('input:radio[name=myradio]');
options.change(enableOther);
//now I want to call the on-change handler to update the initial disabled value
$.proxy(enableOther, options)(); //these don't work
//options.trigger('change');
//options.triggerHandler('change');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button onclick="debugger; init();">Init</button>
<form id="myform">
<ul>
<li><input type="radio" name="myradio" value="1" />Item 1</li>
<li><input type="radio" name="myradio" value="2" />Item 2</li>
<li><input type="radio" name="myradio" value="3" />Item 3</li>
<li><input type="radio" name="myradio" value="other" />Other</li>
<li><input type="text" name="other" /></li>
</ul>
</form>
(点击Init,selectOther,再次点击Init,字段错误的变成disabled)
这在点击每个项目时工作正常,但我希望能够在初始化函数中触发一次我的 .change()
jquery 回调(这个片段进入弹出窗体并持续存在值)。我试过 .proxy
和 trigger
,但我似乎得到了所有单选按钮,.val()
是单独的值,而不是 selected 的值。
我应该如何人为触发此回调?
单选按钮很奇怪。
在您的回调中,使用选中的选择器。
var myval = $('input[type="radio"][name="myradio"]:checked').val();
var other = myval == 'other';
注意:使用 [type="radio"]
而不是 :radio
,因为它可以让选择器在浏览器支持时更有效率。
测试:为了证明概念,在调用 init 之前设置 OTHER:
$('input[type="radio"][name="myradio"]').eq(3).prop('checked',true);
然后不是:
$('input[type="radio"][name="myradio"]').eq(2).prop('checked',true);
然后使用您尝试过的方法:
options.trigger('change');
我正在尝试 enable/disable 一组单选按钮底部的 "other" 字段,就像这样...
window.init = function() {
debugger;
var form = $("#myform");
var enableOther = function() {
var other = $(this).val() == 'other';
form.find('input[name=other]').prop('disabled', !other);
};
var options = form.find('input:radio[name=myradio]');
options.change(enableOther);
//now I want to call the on-change handler to update the initial disabled value
$.proxy(enableOther, options)(); //these don't work
//options.trigger('change');
//options.triggerHandler('change');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button onclick="debugger; init();">Init</button>
<form id="myform">
<ul>
<li><input type="radio" name="myradio" value="1" />Item 1</li>
<li><input type="radio" name="myradio" value="2" />Item 2</li>
<li><input type="radio" name="myradio" value="3" />Item 3</li>
<li><input type="radio" name="myradio" value="other" />Other</li>
<li><input type="text" name="other" /></li>
</ul>
</form>
(点击Init,selectOther,再次点击Init,字段错误的变成disabled)
这在点击每个项目时工作正常,但我希望能够在初始化函数中触发一次我的 .change()
jquery 回调(这个片段进入弹出窗体并持续存在值)。我试过 .proxy
和 trigger
,但我似乎得到了所有单选按钮,.val()
是单独的值,而不是 selected 的值。
我应该如何人为触发此回调?
单选按钮很奇怪。
在您的回调中,使用选中的选择器。
var myval = $('input[type="radio"][name="myradio"]:checked').val();
var other = myval == 'other';
注意:使用 [type="radio"]
而不是 :radio
,因为它可以让选择器在浏览器支持时更有效率。
测试:为了证明概念,在调用 init 之前设置 OTHER:
$('input[type="radio"][name="myradio"]').eq(3).prop('checked',true);
然后不是:
$('input[type="radio"][name="myradio"]').eq(2).prop('checked',true);
然后使用您尝试过的方法:
options.trigger('change');