我不明白js的构造

时间:2016-04-16 16:00:17

标签: javascript

我有代码

 t.prototype.checkFields = function () {
      var e = this,
      t = this.$container.find('.form-fieldset'),
      a = 0;
      return t.filter(':visible').each(function (t, i) {
        var s = $(i),
        n = s.find('input[type="text"], input[type="password"], input[type="email"]'),
        r = n.data('write-id');
        - 1 === e.settings.emptyIgnoring.indexOf(r) && n.length && !n.prop('disabled') && (n.val().trim() || (a++, s.addClass('is-js-error').find('.form-fieldset__error').html('Заполните поле')))
      }),
      a
  },

如果输入为空,该行用.html('Заполнитеполе')写下写文本但我不明白条件何时发生并写入文本,我看不到if (...) then write text

为什么addClass与&&?添加课程时返回什么,是真的吗?

那是什么?

我几乎没有看过原型,在bootstrap和jQuery源代码中看到它

 - 1 === e.settings.emptyIgnoring.indexOf(r)  //indexOf return r position, and is -1 if not found for example one string in another 

 && n.length // that mean that length of n is bigger than 0

 && !n.prop('disabled') //is not set attribute disabled=true is equal i think with attr('disabled')==true

 && (n.val().trim() //removes newlines, spaces and other from the beginning and end of the n value

 || (a++, //increment but why is need here

 s.addClass('is-js-error') //add class name

 .find('.form-fieldset__error') // find child element with class form-fieldset__error

 .html('Заполните поле'))) // set the html to element with class form-fieldset__error

表单提交时发送的帖子请求(电话和电子邮件是空的我也写了$('input')。val('text');发送表单和数据后也是空的)

  token[3853526949671]:4dd0e6e302563d00
  source:add
  captcha:Maxim
  email:asdasd@mm.nn
  private:1
  phone:
  name:
  

我需要填充输入并传递返回输入为空的文本的验证函数,但$('input [type = text]')。val('some text');并提交表单获取输入未填充文本的文本,我认为该功能显示标签和块形式后执行

1 个答案:

答案 0 :(得分:0)

使用一些注释美化您的函数版本 返回包含空文本,密码或电子邮件输入的可见表单字段集的数量 忽略已禁用的输入和emptyIgnoring - 黑名单上的输入。

对代码做了一些细微的修改。

t.prototype.checkFields = function () {
    var ignore = this.settings.emptyIgnoring, 
        numEmptyInputs = 0;

    this.$container.find('.form-fieldset:visible').each(function (i, node) {
        var $node = $(node), 
            $inputs = $node.find('input[type="text"], input[type="password"], input[type="email"]'),
            writeId = $inputs.data('write-id');

        //writeId is not in the Array `settings.emptyIgnoring`
        //this node contains at least one text-, password- or email-input field
        //the input is not disabled (actually only checks the first element)
        //the input contains some none-whitespace characters
        if(ignore.indexOf( writeId ) === -1 && $inputs.length > 0 && !$inputs.prop('disabled') && !$inputs.val().trim()){
            numEmptyInputs++;
            $node.addClass('is-js-error');
            $node.find('.form-fieldset__error').html('Заполните поле');
        }
    }); 
    return numEmptyInputs;
},