2011-11-28 17 views
0

我有这个网站JQuery的获取从单选

<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 &amp; Picture</label> 
</div> 

我想基于erp_report_type更改事件从erp_productformat取值仅第一个值。这是我用它的jQuery。

$("input[name='erp_report_type']").change(function() { 
    var type = $(this).val(); 
    var productFormat = $("input[name='erp_productformat']").val(); 
    if(type == 'customerinvoice') { 

    } else if(type == 'packinglist') { 

    } else if(type == 'performainvoice') { 

    } else if(type == 'commercialinvoice') { 

    } 
}); 

问题是可变的productFormat只能容纳即description如果我检查其他两个领域不取其他值的第一个值。

我仍然是jQuery或JS的noob。我哪里错了?

回答

1

尝试更改为此:

var productFormat = $("input[name='erp_productformat']").filter(':checked').val(); 

这将返回从“erp_productformat”输入组的检查值,那么它的价值。

+0

我需要:检查:),我做到了一次,我忘了:d –

+0

我不认为你甚至需要筛选此。这是我做var'productFormat = $(“input [name ='erp_productformat']:checked”)。val();' –

+0

你不需要'.filter()';你是对的。但是,我永远不会记得在':checked'之前是否需要一个空格,如果你把它放在选择器中。我想给你一些我熟悉的工作,而不是一个不完整的解决方案。 – arb

0

试试这个。

$("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') { 
    } 
}) 
0

尝试的

each(function() { 

代替

change(function() { 
+0

不好主意,我想我的代码被执行使用onchange事件,而不是一切。 –