我试图通过 JQUERY 选择一个单选按钮,但我无法这样做。我尝试使用这些但它不起作用。
$("#agencyBill").prop("checked", true);
$radios.filter('[value=Agency Bill]').prop('checked', true);
$("[name=customRadio]").filter("[value='Agency Bill]']").attr("checked","checked");
here is fiddle link of the same
我需要通过它的值或它的 id 来选择它,任何人都会这样做。
最佳答案
尝试一下:您可以将名称和值属性选择器放在一个中,并使用 prop
而不是 attr
来设置单选选项
$(function() {
$("[name=customRadio][value='Agency Bill']").prop("checked", true);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="custom-control custom-control-inline custom-radio">
<input type="radio" name="customRadio" class="custom-control-input" id="dirBill" value="Direct Bill" checked>
<label class="custom-control-label" for="dirBill">Direct Bill</label>
</div>
<div class="custom-control custom-control-inline custom-radio">
<input type="radio" name="customRadio" class="custom-control-input" value="Agency Bill" id="agencyBill">
<label class="custom-control-label" for="agencyBill">Agency Bill</label>
</div>
<div id="test">
</div>
关于javascript - 通过 jquery 选择单选按钮(按值或按 id),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51552962/