如何确保不应在jquery中添加相同的用户两次? [英] How can I make sure that same user shouldn't be added twice in jquery?

查看:85
本文介绍了如何确保不应在jquery中添加相同的用户两次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用两个div。在第一个div中,所有用户都显示在页面上,带有ADD按钮选项。通过添加用户,他们将使用jQuery添加到第二个div。但我想确保它们不会被添加两次。我想实现一个条件来检查用户是否已经添加,然后不应该添加它或者应该禁用按钮。我正在编写下面的html和css代码。实际上我在wordpress中这样做,但问题在于jquery。我有名为users的页面模板。

  / *   
模板名称:用户
* /

?>

.container {
宽度:800px;
}
.left {
float:left;
宽度:300px;
背景:## 66CDAA;
border-right:thick-solid #fff;
}
.right {
溢出:隐藏;
宽度:500px;
背景:粉蓝;
}

<? php
get_header(); ?>
< div id = primary class = content-area >

< div class = container >
< div class = left id = xyz >






<? php
$ sql = SELECT * FROM wp_users;
$ results = $ wpdb-> get_results($ sql) die(mysql_error( ));
foreach( $ results as $ result ){?>





<? php
}
?>
< > < tbody > < tr > < th > 名称< < span class =code-leadattribute> / th > < th > Gravatar Image < / th > < th > ; 添加到Div < / th > < / tr > < tr > < td id = <?php echo $ result- > ID; ?> >
<? php echo $ result-> display_name; ?>
< ; / td > < td >
< ? php echo get_avatar($ result-> user_email,42); ?>
< / td > < td >
< p > 已添加< / p >
< / td > < / tr > < / tbody > < / table >
< / div >
< div class = right >
< p > 已添加用户< / p >




< table class = table > < tbody > < tr > < th > 名称< / th > < th > ;
< / th > < th > 图片< / th > < / tr > < / tbody > < span class =code-keyword>< / table >
< / div >
< / div >



< / div >
<? php get_footer(); ?>





我的尝试:



我有一个jquery文件用于将用户从第一个div添加到另一个div。但问题是我想在添加用户之前检查一个条件。如果已添加用户,则不应再次添加该用户,而是应提示已添加用户的警报。我的代码在下面



 function ($){
$( document )。ready( function (){
$( 。submit)。on( click function (e){
e.preventDefault();
var id = $( this )。data(' id');
var name = $( this )。data(' name');
var image = $( this )。data(' image' );

if ($(' .table td#' + id)。length){
// 还试过$ ( '表 ')找到(' TD ')ATTR(' ID')。但未定义的结果是
alert( added);
} else {
$(' .table')。append(' ' + name + ' < br>');
// }
})
})
})(jQuery);

解决方案

sql = SELECT * FROM wp_users;


results =


wpdb-> get_results(

I am using two div(s). In the first div, all the users are shown on a page, with "ADD" Button Option. By adding Users, they get added to the second div using jQuery. But I want to make sure they do not get added twice. I want to implement a condition to check if a user is already added, then it should not be added or button should be disabled. I am writing my html and css codes below. Actually I am doing this in wordpress but the problem is in jquery. I have page template named users.

/*
Template Name: Users
*/
?>

.container{
  width: 800px;
}
.left{
  float:left;
  width: 300px;
  background: ##66CDAA;
  border-right: thick-solid #fff;
}
.right{
  overflow: hidden;
  width: 500px;
  background: powderblue;
}

<?php
 get_header(); ?>
 <div id="primary" class="content-area">
   
   <div class="container">
   <div class="left" id="xyz">
     
       
         
         
         
       
     <?php
      $sql ="SELECT * FROM wp_users";
      $results = $wpdb->get_results($sql) or die(mysql_error());
      foreach( $results as $result ){ ?>
        
          
          
          
        
        <?php
          }
        ?>
        <table><tbody><tr><th>Name</th><th>Gravatar Image</th><th>Add to Div</th></tr><tr><td id="<?php echo $result->ID; ?>">
          <?php echo $result->display_name; ?>
          </td><td>
          <?php echo get_avatar( $result->user_email, 42); ?>
          </td><td>
          <p>Added</p>
          </td></tr></tbody></table>
 </div>
 <div class="right">
   <p>Added Users</p>
   
   
     
   
   <table class="table"><tbody><tr><th>Name</th><th>
     </th><th>Image</th></tr></tbody></table>
 </div>
 </div>



     </div>
    <?php get_footer(); ?>



What I have tried:

I have a jquery file using which I am adding user from first div to another. But the problem is that I want to check a condition before adding the user. If a user is already added then it should not be added again, instead a alert should be prompted that "user is already added". My code is below

(function($) {
      $(document).ready(function() {
      $(".submit").on("click", function(e){
      e.preventDefault();
      var id = $(this).data('id');
      var name = $(this).data('name');
      var image = $(this).data('image');

       if($('.table td#' + id).length) {
       //Also tried $('.table').find('td').attr('id'); but undefined is the result
         alert("added");
        } else {
          $('.table').append('' + name + '<br>');
      //  }
  })
})
})(jQuery);

解决方案

sql ="SELECT * FROM wp_users";


results =


wpdb->get_results(


这篇关于如何确保不应在jquery中添加相同的用户两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆