【问题标题】:jquery x-editable numeric inputjquery x-可编辑数字输入
【发布时间】:2013-08-02 12:23:21
【问题描述】:

在 x-editable 库中。如何进行数字输入?那就是不允许输入框里有任何字母。

我试过这个代码

<a class="items" data-name="itemqty" data-title="Enter Item Quantity" data-type="text" href="#" id="itemqty" onkeypress="return isNumberKey(event)"></a>

<script>
    function isNumberKey(evt) {
      var charCode = (evt.which) ? evt.which : event.keyCode;
      if (charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57)) {
          return false;
      } else {
          return true;
      }      
    }
</script>

但它不起作用。如果我使用正常的输入文本,它可以工作,但在 x-editable 中它不起作用。有什么想法吗?

【问题讨论】:

    标签: javascript jquery html x-editable


    【解决方案1】:

    以下解决方案适用于我:

        $('.editable').editable({
            type: 'text', 
            validate: function(value) {
                if ($.isNumeric(value) == '') {
                    return 'Only numbers are allowed';
                }
            }
        });
    

    返回错误字符串很重要(例如“只允许数字”),否则 x-editable 无法识别验证失败。

    【讨论】:

      【解决方案2】:
      $('.jmlcolly,.jumlahitem,.beratsatuan').editable({
          type: 'text', 
          inputclass:'lebar',
          showbuttons:false, 
          title: 'Enter new value' ,
          validate: function(value) {
              if($.trim(value) == '') {
               alert('This field is required');
               return false;
              }
              if ($.isNumeric(value) == '') {
                  alert('This Number is required');
                  return false;
              }
          }
      });
      

      【讨论】:

        【解决方案3】:

        这并不像听起来那么容易。您需要允许其他键,如 EnterTab- 等。

        我建议使用已经使用和测试过的插件,比如Numeric,或者至少看看他们的实现。

        【讨论】:

          【解决方案4】:

          使用data-step="any",而不是step="any"

          例如:

          <span id="cost" data-type="number" data-pk="1" data-title="cost" class="editable editable-click" data-step="any" onclick="callFunctionEditable('cost');">58.54545                                                        </span>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-04-26
            • 2013-05-29
            • 2020-12-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多