【问题标题】:How to allow numbers only in jqGrid cell editing?如何仅在 jqGrid 单元格编辑中允许数字?
【发布时间】:2014-10-15 06:58:38
【问题描述】:

当网格可编辑时,输入时只接受数字怎么做?

JSP 网格加载代码:

<s:url id="mobbillid" action="newmul_mob_gridact" />   
    <sjg:grid caption="EMPLOYEE MOBILEBILL DETAILS"
              gridModel="mobbill_gridModel" 
              height="200"
              href="%{mobbillid}"
              id="gridtab"
              cellEdit="true"
              cellurl="%{mobbillid}"              
              rownumbers="true"
              viewrecords="true"
              pager="true"
              pagerPosition="center"
              navigator="true"
              navigatorSearch="true"
              navigatorSearchOptions="{multipleSearch:true}"
              navigatorDelete="false"
              navigatorEdit="false"
              loadonce="true"
              rowNum="10000"
              multiselect="true"
              reloadTopics="reloadSearchedClaims"
              footerrow="false"
              userDataOnFooter="true"
              onSelectRowTopics="rowselect"
            >
        <sjg:gridColumn name="newsin_mob_faname" index="newsin_mob_faname" title="FAcode" width="100" />
        <sjg:gridColumn name="newsin_mob_name" index="newsin_mob_name" title="FANAME" width="100" />
        <sjg:gridColumn name="newsin_mob_no" index="newsin_mob_no" title="MOBNO" width="100" />
        <sjg:gridColumn name="newsin_mob_billno" index="newsin_mob_billno" title="BILLNO" width="40" editoptions="true" editable="true"  />
        <sjg:gridColumn name="newsin_mob_billamt" index="newsin_mob_billamt" title="BILLAMT" width="90" editable="true" />
        <sjg:gridColumn name="newsin_mob_othchrg" index="newsin_mob_othchrg" title="OTHCHRG" width="95" editable="true" />
        <sjg:gridColumn name="newsin_mob_psts" index="newsin_mob_psts" title="refid" width="70" align="right"  hidden="true"/>
        <sjg:gridColumn name="newsin_mob_rmrk" index="newsin_mob_rmrk" title="REMARK" width="75" align="right"  editable="true"/>
        <sjg:gridColumn name="newsin_mob_opnbal" index="newsin_mob_opnbal" title="OPNBAL" width="75" align="right" hidden="true"/>
        </sjg:grid>  

【问题讨论】:

    标签: javascript jsp jqgrid


    【解决方案1】:

    您可以使用一些 jquery 来处理这个问题,检查一下这个小提琴:http://jsfiddle.net/yrshaikh/44pc78pj/

    你可以让你的可编辑文本框有一个名为.allownumericwithdecimal的类并使用下面的代码

     $(".allownumericwithdecimal").on("keypress keyup blur",function (event) {
         $(this).val($(this).val().replace(/[^0-9\.]/g,''));
                if ((event.which != 46 || $(this).val().indexOf('.') != -1) && (event.which < 48 || event.which > 57)) {
                    event.preventDefault();
                }
            });
    

    如果你的网格文本框是动态添加的,你需要做这样的事情

    $(document).on('keypress keyup blur', '.allownumericwithdecimal', function () {
        $(this).val($(this).val().replace(/[^0-9\.]/g, ''));
        if ((event.which != 46 || $(this).val().indexOf('.') != -1) && (event.which < 48 || event.which > 57)) {
            event.preventDefault();
        }
    });
    

    【讨论】:

    • 你能用这个和你的可编辑文本框设置一个 jsfiddle,然后很想看看它
    【解决方案2】:

    jsp代码:

    <s:url id="mobbillid" action="newmul_mob_gridact" />   
    <sjg:grid caption="EMPLOYEE MOBILEBILL DETAILS"
              gridModel="mobbill_gridModel" 
              height="200"
              href="%{mobbillid}"
              id="gridtab"
              cellEdit="true"
              cellurl="%{mobbillid}"              
              rownumbers="true"
              viewrecords="true"
              pager="true"
              pagerPosition="center"
              navigator="true"
              navigatorSearch="true"
              navigatorSearchOptions="{multipleSearch:true}"
              navigatorDelete="false"
              navigatorEdit="false"
              loadonce="true"
              rowNum="10000"
              multiselect="true"
              reloadTopics="reloadSearchedClaims"
              footerrow="false"
              userDataOnFooter="true"
              onSelectRowTopics="rowselect"
            >
        <sjg:gridColumn name="newsin_mob_faname" index="newsin_mob_faname" title="FAcode" width="100" />
        <sjg:gridColumn name="newsin_mob_name" index="newsin_mob_name" title="FANAME" width="100" />
        <sjg:gridColumn name="newsin_mob_no" index="newsin_mob_no" title="MOBNO" width="100" />
        <sjg:gridColumn name="newsin_mob_billno" index="newsin_mob_billno" title="BILLNO" width="40" editoptions="true" editable="true" editrules="{number:true}"/>
        <sjg:gridColumn name="newsin_mob_billamt" index="newsin_mob_billamt" title="BILLAMT" width="90" editable="true" edittype="text" editrules="{number:true}"/>
        <sjg:gridColumn name="newsin_mob_othchrg" index="newsin_mob_othchrg" title="OTHCHRG" width="95" editable="true"  editrules="{number:true}"/>
        <sjg:gridColumn name="newsin_mob_psts" index="newsin_mob_psts" title="refid" width="70" align="right"  hidden="true"/>
        <sjg:gridColumn name="newsin_mob_rmrk" index="newsin_mob_rmrk" title="REMARK" width="75" align="right"  editable="true" />
        <sjg:gridColumn name="newsin_mob_opnbal" index="newsin_mob_opnbal" title="OPNBAL" width="75" align="right" hidden="true"/>
        </sjg:grid>  
    

    使用editrules="{number:true}"

    当聚焦时,如果它是字母表,则会触发错误消息

    【讨论】:

      猜你喜欢
      • 2020-05-03
      • 1970-01-01
      • 1970-01-01
      • 2013-08-24
      • 1970-01-01
      • 2015-07-03
      • 1970-01-01
      • 2011-03-18
      • 1970-01-01
      相关资源
      最近更新 更多