Jquery复选框麻烦

时间:2013-09-23 11:00:38

标签: javascript jquery checkbox jqgrid

我的jqgrid中有一个按钮。代码为<a class="row-delete" align="left" onclick="deleteItem()"

deleteItem方法是

function deleteItem(){

        if (confirm("Are you sure you want to delete?")) {


            var selected = new Array();
                $('input:checked').each(function() {
                    selected.push($(this).attr('id'));
                }); 

                alert(selected);
            }
            }

这里我试图获取所选复选框的ID。但是正在显示警报消息。在var选择行之前我尝试了alert(hi)。即使这不是印刷。我的代码有什么问题?

我的整个代码是

<script type="text/javascript">
    /* <![CDATA[ */

    function setfocus(){
        document.getElementById("hidden").focus();
    }

    function selectOverride(){
        overrideCheckedFunc();

    }

    function selectLowerRuns(obj){
            if (obj.options[1].selected){
                obj.options[0].selected = true;
            }
        }

    function resetForm(name){
                jQuery('#' + name ).find('input[type=checkbox]:checked').removeAttr('checked');
                jQuery("input[name='programContactForm\:statusRadio']:checked").removeAttr('checked');
                jQuery(".filterKeyword").val("");
        }

    function resetModalPanel(name){
                jQuery(':input','#' + name)
                .not(':button, :submit, :reset, :hidden')
                .val('')
                .removeAttr('checked')
                .removeAttr('selected');
            }


    function checkAll( id, name )
    {
        jQuery( "#" + name + " :checkbox").attr('checked', jQuery('#' + id).is(':checked'));
    }

    function toggleAll(obj,divId){
        if(!obj.checked)
            jQuery("#"+divId).find("input:checkbox").removeAttr('checked');
    }

    function confirm_delete() {
            if (!confirm("Are you sure you want to delete?")) return false;
            return true;
        }

    function deleteItem(){

    if (confirm("Are you sure you want to delete?")) {


            var selected = new Array();
            $('input:checked').each(function() {
                selected.push($(this).attr('id'));
            }); 

            alert(selected);
        }
        }

    var searchUrl = "#{request.contextPath}/seam/resource/restv1/access/search?pid=#{program.id}&ptype=#{program.lkpProgType}";
    var progHide = true;
    var adminHide = true;
    checkBoxHide = false;


    jQuery(document).ready(function() {
        if (#{program.lkpProgType == lookupManager.lkpIdByName('TV_SERIES') })
            progHide = false;
        else
            progHide = true;
    });

    jQuery(document).ready(function() {
        if (#{s:hasRole('ROLE_ADMIN_TITLE_SETUP')})
            adminHide = false;
        else
            adminHide = true;
    });



function gridReload(){
        var jqG= jQuery("#progAccessSearchResults").jqGrid('setGridParam',{
            page:1,
            postData:{
                        'keyword': function(){ return jQuery(".filterKeyword").val();},
                        'distTypeLkpId': function(){

                                var distTypeLkpId = new Array();
                                 jQuery('input:checkbox:checked', "#checkBoxDiv").each(function() {
                                     distTypeLkpId.push(jQuery(this).val());
                                 });                           
                                 return   distTypeLkpId;
                              },
                        'tigresContractStatus' : function(){                                
                                var status = jQuery("input[name='programContactForm\:statusRadio']:checked").val();                                         
                                return status;              
                        },
                        'includeExpired' : function(){ return jQuery(".includeExpire").is(':checked')}
                    }
        }).trigger("reloadGrid");
    }
  function checkBoxChecked() 
{ 
try{

    var checkboxes=document.getElementsByName("test");
//  alert(document.getElementById("mastercheck").checked);
    for(i=0;i<document.getElementsByName("test").length;i++)
    {
     checkboxes[i].checked = "checked";
    }

    }
    catch(e)
    {

    }
    //alert(document.getElementById("mastercheck").checked);
    document.getElementById("mastercheck").checked="checked";
    return true;
} 

     jQuery(document).ready(function() {

         jQuery("#progAccessSearchResults").jqGrid({
                url: searchUrl,
                colNames: ['Id', 'Name','Distr Type','Run No','Start Date','End Date','Access Type','Tigress Status','','Last Updated','Source','<input type="checkbox" onclick="checkBoxChecked()" id="mastercheck"/><a class="row-delete" align="left" onclick="deleteItem()"/>'],
                colModel: [
                            {name:'id', index:'id', hidden : true},
                        //  {name:'season', index:'SEASON', hidden : progHide, width: 80, resizable: false},
                            {name:'name', index:'NAME', width: 120, resizable: false},
                            {name:'distType', index:'DIST_TYPE_NAME', width: 140, resizable: false},
                            {name:'runNo', index:'RUN_NO', hidden : progHide, width: 150, resizable: false},
                            {name:'startDate', index:'B2B_START_DT', width: 90, resizable: false},
                            {name:'endDate', index:'B2B_END_DT', width: 90, resizable: false},
                            {name:'accessType', index:'OVERRIDDEN', width: 80,resizable: false},
                            {name:'contractStatus',index:'contract_status',width:80,resizable:false},
                            {name:'act',index:'act', sortable:false, hidden : adminHide, align:"center", width: 80, resizable: false},
                            {name:'lastUpdated',index:'last_updated_dt', hidden : true},
                            {name:'source',index:'source', hidden : true},
                            {name:'checkbox',index:'checkbox', sortable:false, hidden : adminHide, align:"center", width: 80, resizable: false},
                            //{name: 'checkbox', index: 'checkbox', editable:true, edittype:'checkbox',align:"center",hidden : checkBoxHide, editoptions: { value:"True:False",defaultvalue:"False"}, formatter: "checkbox", formatoptions: {disabled : true} }
                        ],
                        datatype : 'json',
                        mtype: 'POST',
                        width: '920',
                        height: 'auto',
                        rownumbers: false,
                        rownumWidth: 40,
                        sortname : 'last_updated_dt',
                        sortorder : 'desc',
                        scrollOffset : 0,
                        toppager: true,
                        rowNum : jqrowNum,
                        rowList : jqRecordsPerPageList,
                        viewrecords : true,
                        gridview : false,
                        cellEdit : false,
                        caption : 'Search Results',
                        hidegrid : false,
                        pager : jQuery('#progAccessSearchResultsPager'),
                        multiselect : false,
                        toolbar: [true,"top,bottom"],
                        jsonReader: { repeatitems : false, id: "0" },
                        gridComplete: function(){

                        var ids =jQuery("#progAccessSearchResults").jqGrid('getDataIDs');

                        var recs = parseInt(jQuery("#progAccessSearchResults").getGridParam("records"),10);
                        if (recs == 0) {
                            jQuery("#gridWrapper").hide();

                        }
                        else {
                            jQuery('#gridWrapper').show();
                            jQuery(".noResMsg").hide();
                        }
                        },
                });
            // End jqgrid script
     });

    /* ]]> */
    </script>

1 个答案:

答案 0 :(得分:1)

您可能想尝试这个

$('.row-delete').on('click', function() {

   if (confirm("Are you sure you want to delete?")) {
       var selected = new Array();
       $('input:checked').each(function() {
            selected.push($(this).attr('id'));
       });     
       alert(selected);
   }
});

并在html中使用

<a class="row-delete" align="left">click me</a>

在这里摆弄:http://jsfiddle.net/4Umz9/