如何在输入字段中选择所选选项

时间:2012-10-20 01:33:12

标签: jquery

不确定我是否可以正确构建问题。
我有一个可以自定义的页面。所以它包含一堆单选按钮组,文本字段,复选框,下拉列表等。我想要点击按钮,在数组中选择/输入每个字段的所有名称和值。我目前有这个将给我所有输入字段及其值。

$('#submit_btn').click(function(){
         var fields={};
        $(":input").each(function(){
             fields[this.name]=$(this).val();
           //  console.log("%s   - %s",this.name,$(this).val());
         });

所以说我想要包括仅选择了收音机的名称和价值,而不是列出所有单选按钮。知道如何在jQuery中完成它吗?

1 个答案:

答案 0 :(得分:2)

试试这个,

$('#submit_btn').click(function(){
   var fields={};
   $("input[type=radio]:checked").each(function(){
        fields[this.name]=$(this).val();
        //  console.log("%s   - %s",this.name,$(this).val());
   });
});

基于对所有输入的评论和应用已检查无线电的条件。

$('#submit_btn').click(function(){
     var fields={};      
     $(":input").each(function(){
            if($(this).attr('type') == 'radio' && $(this).is(':checked')){            
                fields[this.name]=$(this).val();
            }
     });
});