【问题标题】:If input field is empty fill it with random number如果输入字段为空,则用随机数填充
【发布时间】:2014-07-12 09:36:16
【问题描述】:

我想用随机数填充所有空的数字字段。我可以使用如下所示的随机数填充所有字段。

    $.each(numberField, function () {
        x = Math.floor((Math.random() * 10) + 1);
        ($(this).val(x));
    });

但如果我尝试将它包裹在一个

    if (numberField.val() === "")

没用

我在这里做错了什么? see fiddle

    <input type="number" value="0">
    <input type="number" value="">
    <input type="number" value="4">
    <input type="number" value="5">
    <input type="number" value="">

    var numberField = $('input[type=number]');
    var x = '';
    if (numberField.val() === "") {
        $.each(numberField, function () {
            x = Math.floor((Math.random() * 10) + 1);
            ($(this).val(x));
        });
    }

【问题讨论】:

    标签: javascript jquery html forms random


    【解决方案1】:

    您需要移动您的条件(您正在查看一个数组是否等于“”,但情况并非如此)。您还需要修剪该值以检查它是否真的为空:

    var numberField = $('input[type=number]');
    var x = '';
    
    $.each(numberField, function () {
        if ($.trim($(this).val()) === "") {
            x = Math.floor((Math.random() * 10) + 1);
            ($(this).val(x));
        }
    });
    

    【讨论】:

      【解决方案2】:

      在您的示例中 numberField 是一个数组,因此您的代码必须是这样的:

      var numberField = $('input[type=number]');
      var x = '';
      
          $.each(numberField, function () {
             if ($(this).val() === "") {
                x = Math.floor((Math.random() * 10) + 1);
                $(this).val(x);
              }
          });
      

      【讨论】:

        【解决方案3】:

        请参阅updated fiddle。

        $.each(numberField, function (k, v) {
          if ($(v).val() === "") {
            x = Math.floor((Math.random() * 10) + 1);
            ($(v).val(x));
          }
        });
        

        查看doc 的 jQuery 每个函数。

        【讨论】:

          【解决方案4】:

          试试这个...

          var numberField = $('input[type=number]');
          var x = '';
          
          $.each(numberField, function () {
              if (this.value === "") { // should check the value inside the loop.. not outside
                  x = Math.floor((Math.random() * 10) + 1);
                  this.value = x;
              }
          });
          

          demo

          【讨论】:

            【解决方案5】:

            我认为问题出在这里:

            numberField.val()
            

            改为使用

            $(this).val()
            

            所以...

            var numberField = $('input[type=number]');
            var x = '';
            $.each(numberField, function () {
                alert($(this).val());
                if ($(this).val() === ""){
                    x = Math.floor((Math.random() * 10) + 1);
                    ($(this).val(x));
                };
            });
            

            更新小提琴 http://jsfiddle.net/aaronk85/eC48k/

            【讨论】:

              【解决方案6】:

              您只能匹配没有值的字段,如下所示:

              $('input[type=number][value=""]').each(function () {
                      this.value=Math.floor((Math.random() * 10) + 1);
              });
              

              这是demo。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2011-10-09
                • 1970-01-01
                • 1970-01-01
                • 2018-05-12
                • 2017-11-18
                • 2018-12-08
                • 1970-01-01
                • 2022-01-05
                相关资源
                最近更新 更多