【问题标题】:dataEvents and .change() method not working in jqgrid/jquerydataEvents 和 .change() 方法在 jqgrid/jquery 中不起作用
【发布时间】:2014-05-29 07:41:07
【问题描述】:

我有一个 jqgrid,下面有两个列,OwnerNotifyUsersNotifyUser 取决于 Owner 列的值。就像国家 -> 州 -> 城市。

 {name:'owner',index:'owner',width:80,hidden:false,search:false,align:'center',
        edittype:'select',editable:true},

{name:'notifyUsers',index:'notifyUsers',width:100,hidden:false,search:false,align:'center',
    edittype:'select',editable:true,editoptions:{ value:'${notifiedUserList}', multiple: true}},

我正在做表单编辑。当我们选择任何行时,我有下面的代码来填充这两个 SelectBox。

 ondblClickRow: function(rowid) {               
        $("#searchcriteraigrid").setColProp('owner', {editoptions:{dataUrl:'getUserList.html?critId=' + rowid}});
        $("#searchcriteraigrid").setColProp('notifyUsers', {editoptions:{dataUrl:'getNotifiedUSerList.html?critId=' + rowid, multiple: true}});
)};

到目前为止没有问题。

但我需要根据 Owner 下拉列表的值填充 NotifyUsers 下拉列表。

我尝试了两种方法,例如在 Owner's 列中使用以下代码。

editoptions:{                           
  dataEvents: [
      {
        type: 'change',
        fn: function(e) {
        modifyNotifyUsers(e);
        }
      }
    ]
},

并且 modifyNotifyUsers(e) 某处写成

function modifyNotifyUsers(e){
  var userId = $(e.target).val();
  $.ajax({
      url:"getNotifiedUsersOnOwner.html?userId="+userId,
      type: "get",
      success:function(newOptions){
      var form = $(e.target).closest("form.FormGrid");
      $("select#notifyUsers.FormElement",form[0]).html(newOptions);
   }
 });
} 

这种方法不起作用,因为我认为,我在 Owner 列的 editoptions 中没有 value 属性。 我尝试在editopions 中也使用dataUrl,但我的网址是动态的,这就是为什么我必须将dataUrlondblClickRow() 一起使用

我尝试在更改 Owner 下拉列表时调用简单的 jquery 方法,如下所示。

$('#owner').change(function(){
     $('#searchcriteraigrid').setColProp('notifyUsers',{
      editoptions:{
         dataUrl:'getNotifiedUsersOnOwner.html?crit'+$('#criteriaId').val()
       }
  });       

但是这个方法调用也没有发生。具有讽刺意味的是,当我将页面保存为简单的 HTML 并在浏览器中打开该 HTML 时,就会发生此方法调用。我可以通过在方法中添加一些警报来查看。

您能否帮助我以任何方式使其正常工作,或者建议我做错了什么以及在哪里做错了。

【问题讨论】:

    标签: javascript jquery jqgrid


    【解决方案1】:

    我使用下面的代码解决了它。我不知道.change() 不起作用但.live('change') 起作用的原因是什么。后来我进行了 ajx 调用并动态设置了我的选择框。

    $('#owner').live('change', function() {
        var val = $('#owner').val();
            $.ajax({
              type: "post",
              url: 'getNotifiedUsersOnOwner.html?userId='+val,
              success: function(response) {                 
                $("#notifyUsers").html(response);
              }
           });
    });
    

    如果你有更好的方法来做同样的事情,请指教。谢谢

    【讨论】:

    • live 已在当前 jquery 库中被弃用
    • 你可能需要使用.on函数而不是live
    猜你喜欢
    • 1970-01-01
    • 2012-08-26
    • 1970-01-01
    • 1970-01-01
    • 2018-05-09
    • 2012-06-28
    • 2011-04-24
    • 2016-09-15
    • 2017-11-28
    相关资源
    最近更新 更多