2016-07-08 59 views
0

我使用此代码来获取我检查的项目的值。它不适合我的项目。我只是希望我检查的每个值都会用逗号分隔符 (如288,289,290)发送到我的文本框。在此先感谢复选框值传输到以逗号分隔的文本框

<input type="text" name="holder" id="holder" 
    <input type="checkbox" name="example[]" value="288" checked/> 
    <input type="checkbox" name="example[]" value="289" checked/> 
    <input type="checkbox" name="example[]" value="290" /> 
    <input type="checkbox" name="example1[]" value="289" checked/> 

    var output = jQuery.map($(':checkbox[name="example[]"]:checked'), function (n, i) { 
     return n.value; 
    }).join(','); 

    alert(output); 

回答

0
<!DOCTYPE html> 
<html> 
<head> 
<script src="http://code.jquery.com/jquery-latest.js"></script> 
<meta charset=utf-8 /> 
<title>JS Bin</title> 
</head> 

<body> 
    <div id="inputs"> 
    <input type="checkbox" value="Apple"> 
    <input type="checkbox" value="Orange"> 
    <input type="checkbox" value="Pineapple"> 
    <input type="checkbox" value="Mango"> 
    </div> 
    <input type="text" id="target"/> 
</body> 
</html> 
var arr = []; 
$('#inputs input').change(function() { 
    if (this.checked) { 
    arr.push(this.value); 
    } 
    else { 
    arr.splice(arr.indexOf(this.value), 1); 
    } 
    $('#target').val(arr + ''); 
}); 
+0

我解决我的问题 –

相关问题