JQuery只从radiobutton获取第一个值
我有这个htmlJQuery只从radiobutton获取第一个值,jquery,html,Jquery,Html,我有这个html <div> <input type="radio" name="erp_report_type" value="customerinvoice" required> <label for="">Customer Invoice</label> <input type="radio" name="erp_report_type" value="packinglist"> <label
<div>
<input type="radio" name="erp_report_type" value="customerinvoice" required>
<label for="">Customer Invoice</label>
<input type="radio" name="erp_report_type" value="packinglist">
<label for="">Packing List</label>
<input type="radio" name="erp_report_type" value="performainvoice">
<label for="">Performa Invoice</label>
<input type="radio" name="erp_report_type" value="commercialinvoice">
<label for="">Commercial Invoice</label>
</div>
<div>
<input type="radio" name="erp_productformat" value="description">
<label for="">Description</label>
<input type="radio" name="erp_productformat" value="picture">
<label for="">Picture</label>
<input type="radio" name="erp_productformat" value="descriptionpicture">
<label for="">Description & Picture</label>
</div>
问题是变量productFormat将只保存第一个值,即description
,如果我检查其他两个字段,它不会获取其他值
我仍然是jQuery或JS的不速之客。我哪里出错了?试试这个
$("input[name='erp_productformat']").each(function(){
var productFormat = $(this).val()
if( type == 'customerinvoice') {
} else if(type == 'packinglist') {
} else if(type == 'performainvoice') {
} else if(type == 'commercialinvoice') {
}
})
尝试更改为:
var productFormat = $("input[name='erp_productformat']").filter(':checked').val();
这将从“erp\U productformat”输入组返回选中的值,然后返回其值。请重试
each(function() {
而不是
change(function() {
我需要:勾选:),我做过一次,但我忘了:我认为你们甚至不需要过滤器。这就是我所做的var
productFormat=$(“input[name='erp\u productFormat']:checked”).val()
您不需要.filter()
;你说得对。但是,我永远记不起在:选中之前是否需要空格,或者如果将空格放入选择器中,是否需要空格。我想给你一些我知道有效的东西,而不是一个不完整的解决方案。坏主意,我希望我的代码使用onchange事件执行,而不是所有的东西。
change(function() {