<div class="span6">
<div class="control-group">
<label class="control-label">User Name<span class="required">*</span></label>
<div class="controls">
<?php
foreach($res as $key=>$value){
foreach($value as $ind=>$username){
echo '<div class="controls users">'.form_checkbox('username[]', $username).$username.'</div>';
}
}
?>
</div>
</div>
</div>
<div class="span6">
<div class="control-group">
<label class="control-label">Programs<span class="required">*</span></label>
<?php
$ids = explode(',', $program_id);
foreach($programs as $value){
?>
<div class="controls programs">
<?php echo '<label class="radio">'.form_radio('programs', $value->id).$value->program_name.'</label>' ?>
<div class="listings <?php echo $value->id ?>"></div>
</div>
<?php } ?>
<input type="button" name="reset" value="Reset" id="reset"/>
<input type="button" name="submit" value="Ok" id="oksubmit"/>
</div>
</div>
我有用户和程序。我想将用户添加到程序中,我正在使用 jquery。我已经向程序中添加了多个用户。问题是我不想为同一个程序添加两次用户。怎么做。我在 jQuery 中尝试了以下但无济于事.
<script type="text/javascript">
$(document).ready(function(e) {
$('.programs div.listings').slideUp();
$('#oksubmit').click(function(){
var error = false;
var temp = '';
var selusers = $('div.users span.checked input').map(function(_,el){
return $(el).val();
}).get();
var prgm = $('div.programs span.checked input').val();
if(selusers == ''){
alert('Select at least 1 user.');
var error = true;
return false;
}
if(prgm == ''){
alert('Select 1 program.');
var error = true;
return false;
}
if( ! error){
$('div.'+prgm).slideDown();
$('div.users span.checked').each(function(){
var value=$(this).find('input[type="checkbox"]').val();
if($('div.'+prgm).children().length > 0){
$('div.'+prgm).children().each(function(){
var childval = $('div.list input').val();
if(value == childval){alert('a');
alert('Error');
var error = true;
return false;
}else{
alert('n');
}
})
if( ! error){
temp += '<div class="list"><input type="text" value="'+value+'" readonly></input><a class="delete" id="'+value+'">X</a></div>';
}
}else{
temp += '<div class="list"><input type="text" value="'+value+'" readonly></input><a class="delete" id="'+value+'">X</a></div>';
}
})
$('div.'+prgm).append(temp);
}
});
</script>
欢迎任何建议/帮助。在此先感谢。
我的解决方案:
$('#oksubmit').click(function(){
$('div.users span.checked').each(function(){
var flag = true;
var value = $(this).find('input[type="checkbox"]').val();
var child = $('div.'+prgm).find('.list').size();
var temp = '<div class="list"><input type="text" name="selusers['+prgm+'][]" value="'+value+'" readonly></input><a class="delete btn-danger" id="'+value+'"><i class="icon-remove icon-white"></i></a></div>';
$('div.'+prgm).find('div.list').each(function(ind,val){
var childval = $(this).find('input').val();
if(value == childval){
flag =false;
alert('Cant add '+value+' twice in same program');
return false;
}
});
if(flag){
$('div.'+prgm).append(temp);
}else{
//alert('b')
}
})
})