I write a project on Yii2, and I make one button, “Rate”, the user clicks on it, and the ajax should transfer the data to the controller and the action, everything works fine in opera and chrome, but Firefox does not give in to any. That is, in the firefox user throws a link to this action right in the browser. And yes, probably googled the entire Internet, did not help. The version of Firefox is 52.0.1. Has anyone come across this at all? Here are all the variations that I have already done, and which did not lead to success.

$('.like-btn').on('click', function(e) { e.preventDefault(); $('.like-btn').on('click', function(e) { e = $.event.fix(e); e.preventDefault(); $('.like-btn').on('click', function(event) { event.preventDefault ? event.preventDefault() : (event.returnValue = false); 

And of course the option where at the end return false; (also did not work)

Yii2:

 <?= Html::a(Html::button(Html::img(Yii::getAlias("@web") . '/images/like-unactive.png', ['width' => '20px', 'class' => 'like-btn', 'data-id' => $photo->id ]), ['class' => 'floating-like', 'name' => 'like', 'value' => $photo->id]), ['profile/like', 'id' => $photo->id]) ?> 

HTML:

 <a href="/profile/like?id=50"> <button type="button" class="floating-like" name="like" value="50"> <img class="like-btn" src="/images/like-unactive.png" width="20px" alt="" data-id="50"> </button> </a> 
  • should work. Does the code get into the handler at all? - Igor
  • if you mean if the JS is being loaded, then yes, I looked at the detector, and the code is the one that I described in one of the examples. Why doesn’t it work to attach my mind at all I can’t - idriverx
  • I meant, whether function(e) { ... is fulfilled. Insert there the first line alert("Click"); . - Igor
  • You know what is strange, but it really does not fall. Alert is not displayed. For what reason could it be at all? - idriverx
  • one
    Why did you even need to preventDefault when you click on the picture? - vp_arth

1 answer 1

As you truly spotted @vp_arth, you assign a handler for img , but the event pops up to а .

 $('.like-btn').closest('a').on('click', function(e) { e.preventDefault(); ... }); 
  • It is finished! Thank you very much! But the question of why it worked in two browsers, and in one not - remains open to me) - idriverx
  • one
    @idriverx yes, I agree. I would also like to understand. As a first guess: FF handles clicks on items inside links in a different way, which in the standard case should lead to loading another page. - Igor
  • I also apologize, but I think this record looks too complicated? Is it possible to simplify it? I just made a picture inside the button, so that there was not a standard button, but the desired image. And because the entry where the 3 nested tags seem to me very not very - idriverx
  • one
    @idriverx, just transfer the selector class directly to the link. - vp_arth
  • one
    @idriverx is not entirely clear why you need a link that does not look like a link and does not work as a link. Is it not enough to leave just a button with a picture, and hang up the handler on the button? - Igor