在php jqGrid中使用单选按钮

时间:2014-09-19 12:32:21

标签: javascript php html 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上的单选按钮。但是一旦按下输入,单选按钮值就不会传递到我的编辑页面。这是可以理解的coz myval函数是读取div元素而不是无线电元素。由于有两个单选按钮(具有相同的名称,所以在一点上只能检查一个是/否),并且它不是像文本框那样的单个实体,为了“返回”myelem我必须使用div 。有没有办法我可以在这里获取无线电元素值?

1 个答案:

答案 0 :(得分:0)

我已经弄清楚了..我需要做的就是:

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