jquery - twitter bootstrap modal won't work on first call -


i'm practicing twitter bootsrap , made example website. use modal view display warnings when users vote. won't display warning when vote first time. displays second time. if go my test site , click on arrow see nothing displayed. when click 1 more time see modal message.(which saying have logging vote in language) cannot figure out why it's happening. help.

this jquery.

<script> jquery(document).ready(function($){                $('.upvote').click( function(event) {                event.preventdefault();                var voteid = $(this).attr("id");                  $.ajax({                   url: "/ajax.php?page=votetitle",                   type: "post",                   datatype: "json",                   data: {id : voteid, vote: '1'},                   success: function(data){                     if(data.success == 'true'){                       $('#voteresponse'+voteid).html(data.message);                     }else{                       $(".upvote").attr("data-toggle", "modal");                       $(".upvote").attr("data-target", "#modal");                                        $('#modal').modal('show');                       $('#modal').on('show', function () {                         $('.modallabel',this).html('oy verirken hata oluştu!').css({color: 'red'});;                         $('.modal-body',this).html(data.message);                       });                     }                   },                   error:function(){                       $('#voteresponse').popover({                         title: 'hata!',                         content: 'server hatasi'                        });                   }                 });                });                $('.downvote').click( function(event) {                event.preventdefault();                var voteid = $(this).attr("id");                  $.ajax({                   url: "/ajax.php?page=votetitle",                   type: "post",                   datatype: "json",                   data: {id : voteid, vote: '2'},                   success: function(data){                     if(data.success == 'true'){                       $('#voteresponse'+voteid).html(data.message);                     }else{                       $(".downvote").attr("data-toggle", "modal");                       $(".downvote").attr("data-target", "#modal");                                        $('#modal').modal('show');                       $('#modal').on('show', function () {                         $('.modallabel',this).html('oy verirken hata oluştu!').css({color: 'red'});;                         $('.modal-body',this).html(data.message);                       });                     }                   },                   error:function(){                       $('#voteresponse').popover({                         title: 'hata!',                         content: 'server hatasi'                        });                   }                 });                });             }); </script> 

modal html

    <div id="modal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="mymodallabel" aria-hidden="true">       <div class="modal-header">         <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>         <h3 class="modallabel"></h3>       </div>       <div class="modal-body">        </div>       <div class="modal-footer">         <button class="btn" data-dismiss="modal" aria-hidden="true">kapat</button>       </div>     </div> 

and buttons

<button type="submit" class="btn btn-mini upvote" id="'.$data['contentid'].'"><i class="icon-arrow-up"></i></button>  <span id="voteresponse'.$data['contentid'].'">'.intval(  $data['totalvotes'] - $data['votesum']  ).'</span> <button type="submit" class="btn btn-mini downvote" id="'.$data['contentid'].'"><i class="icon-arrow-down"></i></button> 

because buttons don't have data-toggle="modal" attribute before clicking. buttons attribute after first click , can call modal on second click.

you can try removing these 2 lines:

 $(".downvote").attr("data-toggle", "modal");  $(".downvote").attr("data-target", "#modal"); 

and instead call modal javascript:

$('#modal').modal('show'); 

make sure put after $('#modal').on('show - function..so this:

$('#modal').on('show', function () {     $('.modallabel',this).html('oy verirken hata oluştu!').css({color: 'red'});;     $('.modal-body',this).html(data.message); }); $('#modal').modal('show'); 

as tallmaris suggested in comments, placing event listener in click handler isn't best solution, besides don't think need listener, - change this:

$('#modal').on('show', function () {     $('.modallabel',this).html('oy verirken hata oluştu!').css({color: 'red'});;     $('.modal-body',this).html(data.message); }); $('#modal').modal('show'); 

into this:

$('.modallabel').html('oy verirken hata oluştu!').css({color: 'red'});; $('.modal-body').html(data.message); $('#modal').modal('show'); 

Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -