2016-12-11 41 views
1

我有这样的HTML和需要对表格提交到获得一个结果值的所有检查的值,但我利用图书馆iCheck.js,我没有投入使用属性检查得到所有检查I检查输入值,当表单提交

$('.checkbox-buy').iCheck({ 
 
    checkboxClass: 'icheckbox_square-green', 
 
    radioClass: 'iradio_square-green', 
 
    increaseArea: '20%' // optional 
 
}); 
 

 
/* I need something like but it doesn't work 
 
*/ 
 
$('#form1').submit(function(){ 
 
    var result; 
 
    $('.checkbox-buy').on('ifChecked', function(event).each(function(){ 
 
result += $(this).val(); 
 
}); 
 
});
<script src="http://tvoetango.ru/elki2/libs/icheck/icheck.min.js"></script> 
 

 
<link href="http://tvoetango.ru/elki2/libs/icheck/skins/square/green.css" rel="stylesheet"/> 
 

 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> 
 
<form action="form.php" method="post" id="form1"> 
 
\t <input type="checkbox" name="buy-item" class="checkbox-buy" value="value 1"> 
 
\t <input type="checkbox" name="buy-item" class="checkbox-buy" value="value 2"> 
 
\t <input type="checkbox" name="buy-item" class="checkbox-buy" value="value 3"> 
 
    <!-- 
 
In html I have such code 
 
--> 
 
    <!-- 
 
    <div class="icheckbox_square-green checked" style="position: relative;"> 
 
<input class="checkbox-buy" name="buy-item" value="value1" style="position: absolute; top: -20%; left: -20%; display: block; width: 140%; height: 140%; margin: 0px; padding: 0px; background: rgb(255, 255, 255) none repeat scroll 0% 0%; border: 0px none; opacity: 0;" type="checkbox"> 
 
<ins class="iCheck-helper" style="position: absolute; top: -20%; left: -20%; display: block; width: 140%; height: 140%; margin: 0px; padding: 0px; background: rgb(255, 255, 255) none repeat scroll 0% 0%; border: 0px none; opacity: 0;"></ins> 
 
</div> 
 
--> 
 
</form>

+0

什么你说的意思是 “*的一个结果值*”?你是否需要用逗号分隔的列表将它们复制到不同的输入字段,JavaScript数组中,使用JSON还是其他? – insertusernamehere

+0

我添加更多的ditails。我需要检查复选框的所有值。无论在哪种类型 – Valentina

回答

2

我旁边的html:

<input type="checkbox" name="buy-item" class="checkbox-buy" value="value 1" checked="true"> 
<input type="checkbox" name="buy-item" class="checkbox-buy" value="value 2"> 
<input type="checkbox" name="buy-item" class="checkbox-buy" value="value 3"> 

得到检查值作为数组值:

var dic = $(".checkbox-buy:checked").map(function(){ 
    return $(this).val(); 
}).toArray(); 

console.log(dic); // => ["value 1"] 

Demo

+0

谢谢它帮助我 – iCoders

0

试试这个:

$('#form1').submit(function(){ 
    var result = ''; 
    $(this).find('.checkbox-buy').each(function() 
    if($(this).prop("checked") == true) { 
     result += $(this).val(); 
    } 
    }); 
}); 
0

下面的代码工作对我来说,

$('#form1').submit(function(){ 
    var result = ''; 
    $.each($("input[name='buy-item']:checked"), function(){ 
     result += $(this).val(); 
    }); 
});