2014-09-19 101 views
0

我有一個使用php的jqgrid。在其中一列中,我想使用兩個單選按鈕(是/否)。我爲此使用了自定義格式化程序。在php中使用單選按鈕jqGrid

$(function() { 
    function myelem (value, options) { 
    var radio1 = document.createElement('input'); 
    radio1.id = 'radY'; 
    radio1.type = 'radio'; 
    radio1.name = 'appr'; 
    radio1.value = 'Yes'; 

    var radio2 = document.createElement('input'); 
    radio2.id = 'radN'; 
    radio2.type = 'radio'; 
    radio2.name = 'appr'; 
    radio2.value = 'No'; 

    var label1 = document.createElement('label'); 
    label1.setAttribute('for', radio1.id); 
    label1.innerHTML = 'Yes'; 

    var label2 = document.createElement('label'); 
    label2.setAttribute('for', radio2.id); 
    label2.innerHTML = 'No'; 

    var container = document.createElement('div'); 
    container.appendChild(radio1); 
    container.appendChild(label1); 
    container.appendChild(radio2); 
    container.appendChild(label2); 

    return container; 
    } 

    function myvalue(elem, operation, value) { 
    if(operation === 'get') { 
     return $(elem).val(); 
    } 
    else if(operation === 'set') { 
     $('input',elem).val(value); 
    } 
    } 

    $("#list").jqGrid({ 
    url: "fetch.php?sqlselect=1", 
    datatype: "json", 
    mtype: "GET", 
    colNames: ["Role", "Region", "Approved"], 
    colModel: [ 
     { name: "role", width: 60,editable:true}, 
     { name: "region", width: 60,editable:true}, 
     { name: "approved", width:250, editable:true, edittype:"custom", editoptions: {custom_element: myelem, custom_value:myvalue}} 
    ], 
    onSelectRow: function(id){ 
     if(id && id!==lastSel){ 
     jQuery('#list').restoreRow(lastSel); 
     lastSel=id; 
     } 
     jQuery('#list').jqGrid('editRow',id, 
     { 
    keys : true, 
    url : "edit.php" 
     }); 
    }, 

    ............. 

編輯時除了單選按鈕列以外,一切正常。我可以看到onClick的單選按鈕。但一旦我按下輸入單選按鈕的值不會傳遞到我的編輯頁面。這是可以理解的因爲myval函數讀取div元素而不是無線電元素。由於有兩個單選按鈕(具有相同的名稱,因此在點上只能檢查一個是/否),並且它不是像文本框那樣的單個實體,爲了'返回'myelem我必須使用div 。有沒有辦法我可以做到這一點,我可以獲取無線電元素值?

回答

0

我已經想通了。所有我需要做的是這樣的:

function myvalue(elem, operation, value) { 
    if(document.getElementById('radY').checked) { 
     return "Yes"; 
    } 
    else { 
     return "No"; 
    } 
}