首页 > 解决方案 > 从函数jquery中的函数获取值

问题描述

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

(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);
});

标签: javascriptjqueryfunctionreturn

解决方案


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


推荐阅读