最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery isEmpty is not working - Stack Overflow

programmeradmin1浏览0评论

Why this jQuery if statment is not working ?

if (isEmpty($('input.user'))) {
    alert ('123123');
}

This is reffering to input box with class of .user so basically if user left it empty alert.

Very basic but not working.

Why this jQuery if statment is not working ?

if (isEmpty($('input.user'))) {
    alert ('123123');
}

This is reffering to input box with class of .user so basically if user left it empty alert.

Very basic but not working.

Share asked Apr 7, 2014 at 15:58 puactionpuaction 2113 silver badges12 bronze badges 6
  • 2 Please show us the code for isEmpty... it's not a built in function. It might be expecting the value of input.user rather than the jQuery object, so you could try isEmpty($('input.user').val()). – Matt Commented Apr 7, 2014 at 15:59
  • $('input.user').val() === "" or $('input.user').val().length === 0 – blgt Commented Apr 7, 2014 at 16:00
  • 1 stackoverflow./questions/4597900/… – Blazemonger Commented Apr 7, 2014 at 16:00
  • This is just a simple <input name=user type=text> – puaction Commented Apr 7, 2014 at 16:00
  • @blgt yes i know this method and it was working for me but i was looking for cleaner method that is why i asked – puaction Commented Apr 7, 2014 at 16:01
 |  Show 1 more ment

4 Answers 4

Reset to default 6

In jquery if you want to check if a form input is empty you can do it in this way:

if ( ! $('input.user').val()) {
    alert ('123123');
}

The method .val() will return false if the input element is empty.

Use .is() and :empty

if ($('input.user').is(':empty')){
     alert ('I am Empty'); // alert ('123123');
}


if ($('input.user').val() === ''){ }


Or

if ($.trim($('input.user').val()) === ''){ }


Or

if ($.trim($('input.user').val()).length === 0){ }

There is no built-in JavaScript function for isEmpty.

If you want to check if a field is empty using jQuery you should use something like:

if (!$('input.user').val()){
   // DO SOMETHING
   alert ('123123');
}

Here's a jsFiddle that shows you how to check if a value is present on clicking a button - http://jsfiddle/jEte6/

To do the same using pure Javascript (No libraries)

If you're testing for an empty string:

if(myVar === ''){
    // Do something
}

If you're checking for a variable that has been declared, but not defined:

if(myVar === null){
    // Do something
}

If you're checking for a variable that may not be defined:

if(myVar === undefined){ 
    // Do something
}

In my case, the problem was that I was including jQuery twice in my html. Check your html and if there are two, delete one and the problem should be fixed.

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>