Javascript Jquery从选中的Jquery对象集合中进行选择
我还没有找到这个答案。我不知道是搜索错误还是词典不正确,但我正在尝试获取一组jquery单选按钮的选定元素。以下是我的JS代码:Javascript Jquery从选中的Jquery对象集合中进行选择,javascript,jquery,html,Javascript,Jquery,Html,我还没有找到这个答案。我不知道是搜索错误还是词典不正确,但我正在尝试获取一组jquery单选按钮的选定元素。以下是我的JS代码: var hideFieldsBasedOnRadioButtonValue = function () { var $employmentStatusRadioButtons = $(".edit-profile-employment-information input[name='user[profile_attributes][employment_stat
var hideFieldsBasedOnRadioButtonValue = function () {
var $employmentStatusRadioButtons = $(".edit-profile-employment-information input[name='user[profile_attributes][employment_status]']");
var displayOrHideRequiredFields = function ($radioButton) {
if ($radioButton.val() === "Full Time" || $radioButton.val() === "Part Time") {
$radioButton.closest("div.form-row").next(".required-input-when-yes").removeClass("d-none");
} else {
$radioButton.closest("div.form-row").next(".required-input-when-yes").addClass("d-none");
}
}
displayOrHideRequiredFields($(".edit-profile-employment-information input[name='user[profile_attributes][employment_status]']:checked"));
$employmentStatusRadioButtons.on("change", function () {
displayOrHideRequiredFields($(this));
});
}
这是此函数的精简版本。我还有几个单选按钮,需要根据单选按钮的值隐藏或显示其他字段。具体来说,我遇到的问题是我想把这一行删减:
displayOrHideRequiredFields($(".edit-profile-employment-information input[name='user[profile_attributes][employment_status]']:checked"));
$employmentStatusRadioButtons是jquery对象的集合,我不知道如何获取所选对象。任何帮助都将是惊人的
不要在代码的这一行和第2行使用相同的“.edit profile employment information input[name='user[profile\u attributes][employment\u status]]”选择器,我想执行类似操作
displayOrHideRequiredFields($employmentStatusRadioButtons:checked);
现在这行不通了。我所能做的最接近于实现这一目标的方法是:
displayOrHideRequiredFields($employmentStatusRadioButtons.selector + ":checked")
但我真的不喜欢它的样子。我们不使用ES6,否则它会很棒
在这方面我找不到任何帮助。有人知道我可以在哪些方法中执行类似操作吗?您可以使用jquery函数过滤带有jquery对象的变量
var$radios=$('[name=test],[name=test2]');
变量$radiosFiltered=$radios.filter(“:选中”);
$radiosFiltered.each(函数(){
log($(this.val());
});代码>
您非常接近,需要使用过滤器从集合中获取选中的元素:
$employmentStatusRadioButtons.filter(':checked');
您可以执行$employmentStatusRadioButtons.filter(':checked')代码>@MarkBaijens我爱你。成功了。谢谢谢谢,伙计!我知道你也很接近,但马克比你先一步,所以我不得不接受他的建议。谢谢你!!