2016-07-05 4 views
1

私は、ユーザリンク上のすべてのユーザの個人IDをhtmlで警告するようにしています。したがって、すべてのユーザーはデータベースからフェッチされ、リンクとしてページに表示されます。また、彼らは一意の電子メールIDを持っているので、私はそのユーザーリンクがクリックされたときに電子メールを警告しようとしています。私の問題は、最初のものをクリックするたびに電子メールが警告されますが、クリックすると2番目、3番目などは警告が表示されません。ここでその後のアンカータグがhtml/phpで動作しない

は、コードの抜粋です:

<strong><a href="#" id="post_user" name="post_user" class="<?php echo $messagePost_Email; ?>"><?php echo $messagePost_FirstName.' '.$messagePost_LastName;?></a></strong> 

のjQuery

<script type="text/javascript"> 

    $(function(){ 
    $("#post_user").click(function(){ 
     var element = $(this); 
     var emailID = element.attr("class"); 
     var info = "emailID="+emailID; 
     alert(info); 
     $.ajax({ 
     tpye : "POST", 
     url : "navigate.php", 
     data: info, 
     success : function(){ 

     } 
     }) 
    }) 
    }) 

</script> 

私はの根本的な原因を把握することはできませんので任意の提案は、大きな助けになるであろうこの動作。

答えて

1

を試してみてください:id属性は識別子であることを意味する

<strong><a href="#" id="post_user" ... 

は一意である必要があります

id属性を削除する必要があります(他の場所で使用しない場合)。代わりに、classをjQueryセレクタとして使用してください。例:

<strong><a href="#" class="post_user" ... 

それは、そのような物事のより明確な方法だとして、要素のdata attributeemailID(ユーザーID)店。これは、data属性が最もよく使用されるものです。

<a href="#" class="post_user" data-emailID="<?php echo $messagePost_Email; ?>" ... 

のjQuery:

$(function(){ 
    $(".post_user").click(function(){ 
     var emailID = $(this).attr("data-emailID"); 
     // or: 
     // emailID = $(this).data("emailID"); 
     alert(emailID); 
     $.ajax({ 
      type : "POST", 
      url  : "navigate.php", 
      data : {emailID : emailID}, 
      success : function(response){ 
       // ... do anything with "response" ... 
      } 
     }) 
    }) 
}) 
+0

は、魅力的な働きをしてくれた、ARTURに感謝します。 –

+0

私の他の質問は、$ _POSTは名前属性値しか取らないので、ここでjqueryを使用する代わりにdata-emailID属性の値を取得したい場合です。 –

+0

HTML5の場合、アンカータグの 'name'属性は無効です。 'input' /' select'/'checkbox'/etcなどのフォーム要素に使うことができます。 –

0

問題は、おそらくすべてのアンカー要素に同じid属性使用していることである。この方法

<script src="jquery.min.js"></script> 
<strong><a href="#" id="post_user1" name='[email protected]'>user1</a></strong> 
<strong><a href="#" id="post_user2" name='[email protected]'>user2</a></strong> 
<strong><a href="#" id="post_user3" name='[email protected]'>user3</a></strong> 

<script type="text/javascript"> 

$("a").click(function(event){ 
    // Capture the href from the selected link... 
    ids = ['post_user1', 'post_user2'];//add anchor id, at which you want to call this function. 
    var id = this.id; 
    if($.inArray(id, ids) != -1) {// check this id is in list. 
     var email = this.name; 
     var info = "emailID="+email; 
     alert(email); 
     $.ajax({ 
      tpye : "POST", 
      url : "navigate.php", 
      data: info, 
      success : function(){ 

     } 
     }); 
    } 
    return false; // not needed if you want the link to execute normally... 
}); 

</script> 
+0

([?なぜHTMLでのonClick()悪い習慣を使用しています] http://stackoverflow.com/questions/5871640/why-is-using-onclick- in-html-a-bad-practice) –

関連する問題