从函数内部获取值jquery [duplicate]

问题描述 投票:0回答:1

这个问题在这里已有答案:

我真的很难从这个函数中获取一个值,我的猜测是因为函数内部的函数。

(ps。我知道当我想获得BOOL值时我不必使用引号)

请帮忙

$(document).ready(function() {    
  var text = null;

  function textCheck() {
    if ($('.window-' + sc).has('input[type="text"]')) {
      $(this).find('input[type="text"]').each(function() {    
        $(this).on('click', function() {    
          if ($(this).attr('required')) {
            var placeholder = $(this).attr('original');

            if (!$(this).val() == '') {
              $(this).removeClass('is-invalid shake').addClass('is-valid').attr('placeholder', placeholder);
              text = 'true';
              //return text;    
            } else {
              $(this).addClass('is-invalid shake').attr('placeholder', placeholder + " can't be empty");
              text = 'false';
              //return text;    
            };
          };

        });
      });
    };
    return text;
  };

  var texxt = textCheck();
  console.log(texxt);
});
javascript jquery function return
1个回答
1
投票

在你的textCheck()函数中,第一个$(this)代表窗口 将此更改为您的元素

© www.soinside.com 2019 - 2024. All rights reserved.