简体   繁体   English

似乎无法弄清楚为什么使用jquery单击按钮后我没有收到警报

[英]Can't seem to figure out why am I not getting an alert after clicking a button using jquery

Long story short, up until now I have only been using html, css and php for my website. 长话短说,到目前为止,我只在我的网站上使用html,css和php。 I added the ability to be able to upvote and downvote images, however, since I don't want the page to refresh every time I upvote or downvote, I did a quick search and found out I'd have to use jquery. 我添加了能够对图像进行增删减的功能,但是,由于我不想每次增删减收时都刷新页面,因此我进行了快速搜索,发现必须使用jquery。 After including my jquery and javascript files in the footer, I decided to write a simple piece of code to see if I'll get an alert when I click the upvote button. 在页脚中包含我的jquery和javascript文件之后,我决定编写一段简单的代码,以查看单击“ upvote”按钮时是否会收到警报。 Here's the code 这是代码

$('.arrowUp').click(function(){
    alert('hello');
});

Unfortunately, when I click the upvote button, the alert doesn't pop up for some reason so I can only assume that whatever code I write in that function, it's not gonna work until I figure out why is the alert not popping. 不幸的是,当我单击“ upvote”按钮时,由于某种原因警报不会弹出,因此我只能假设我在该函数中编写的任何代码都无法正常工作,直到我弄清楚为什么警报没有弹出。

This is my upvote button code. 这是我的投票按钮代码。

if ($hasVoted < 1) {
            echo    "<div class='upvoteDownvoteRatingContainer'><form class='upvoteImage' method='POST' action=''>
                        <input type='hidden' name='action' value='upvote'>
                        <input type='hidden' name='id' value='".$row['id']."'>
                        <input type='hidden' name='userId' value='".$currentUser."'>
                        <button class='upvoteImageButton' type='submit' name='upvoteImage'><img class='arrowUp' src='../images/Social Media/arrowUp.png' alt='submit'></button>
                    </form>";
                    </div>
}

Providing that jQuery is properly included, I think the problem lies in the <img> which is in a type="submit" <button> , which is inside a form. 假设已正确包含jQuery,我认为问题出在<img> ,它位于type="submit" <button> ,位于表单内。 Naturally, when you click the submit button it tries to submit the form. 自然,当您单击提交按钮时,它将尝试提交表单。

 $('.arrowUp').click(function() { alert('test'); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <div class='upvoteDownvoteRatingContainer'> <form class='upvoteImage' method='POST' action=''> <input type='hidden' name='action' value='upvote'> <input type='hidden' name='id' value=''> <input type='hidden' name='userId' value='currentUser'> <button class='upvoteImageButton' type='submit' name='upvoteImage'><img class='arrowUp' src='../images/Social Media/arrowUp.png' alt='submit'></button> </form> </div> 

As you can see in the example above, the alert doesn't happen. 如您在上面的示例中看到的,警报不会发生。 Now, with the exact same code except the <img> outside the button, 现在,随着除了完全相同的代码<img>的按钮

 $('.arrowUp').click(function() { alert('test'); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <div class='upvoteDownvoteRatingContainer'> <form class='upvoteImage' method='POST' action=''> <input type='hidden' name='action' value='upvote'> <input type='hidden' name='id' value=''> <input type='hidden' name='userId' value='currentUser'> <img class='arrowUp' src='../images/Social Media/arrowUp.png' alt='submit'> <button class='upvoteImageButton' type='submit' name='upvoteImage'></button> </form> </div> 

Edit: 编辑:

It appears the OP had a script error which was causing the error, which has now been fixed. 看来OP的脚本错误导致了该错误,现已修复。

add the arrowUp class to <button> instead of <ima> arrowUp类添加到<button>而不是<ima>

 <div class='upvoteDownvoteRatingContainer'><form class='upvoteImage' method='POST' action=''>
                    <input type='hidden' name='action' value='upvote'>
                    <input type='hidden' name='id' value='".$row['id']."'>
                    <input type='hidden' name='userId' value='".$currentUser."'>
                    <button class='upvoteImageButton arrowUp' type='submit' name='upvoteImage'>
                    <img src='../images/Social Media/arrowUp.png' alt='submit'></button>
                </form>";
                </div>

Put your code in inside $(document).ready(function(){}); 将您的代码放入$(document).ready(function(){}); .

Put the arrowUp class in button. 将arrowUp类放入按钮。

<button class='upvoteImageButton arrowUp' type='submit' name='upvoteImage'>
      <img src='../images/Social Media/arrowUp.png' alt='submit'>
</button>

Or Place the image alongside button not inside of it. 或将图像放置在按钮旁边而不是按钮的内部。

And of course wrap you js code like: 当然,将您的js代码包装为:

$(document).ready(function(){
  $('.arrowUp').click(function(){
    alert('hello');
  });
});

在Jquery选择器中选择.upvoteImageButton而不是.arrowUp.arrowUp ,您确定要在加载文档后加载jquery。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我在JavaScript中变得未定义,无法弄清为什么 - I am getting undefined in JavaScript and can't figure it out why 我似乎无法弄清楚为什么第三个函数在其他两个函数之前运行,即使我使用的是 Promises。 我究竟做错了什么? - I can't seem to figure out why the third function runs before the other two even though I am using Promises. What am I doing wrong? 我不知道为什么我收到以下错误“错误:元素类型无效:” - I can't figure out why I am getting the following error "Error: Element type is invalid:" 在 API 的帮助下创建网页时,无法弄清楚为什么我的终端会出现此错误 - Can't figure out why I am getting this error in my terminal while creating a webpage with the help of API 我尝试为 createUser 创建一个可调用的函数,但是我在运行它时遇到了一个错误,我不知道为什么 - I tried making a callable function for createUser but I am getting an error wen I run it and I can't figure out why 似乎无法弄清楚该脚本为何不起作用 - Can't seem to figure out why the script isn't working 无法弄清楚为什么我一直不确定 - Can't figure out why I keep getting undefined 我的javascript函数似乎无法正常工作。 我不知道为什么 - My javascript function doesn't seem to be working. I can't figure out why jQuery中的错误由于某种原因,我不知道为什么 - ERROR in jQuery for some reason and I can't figure out why 我不断收到“未捕获的TypeError:无法读取null的属性&#39;value&#39;”响应,并且无法弄清原因 - I am continually getting a 'Uncaught TypeError: Cannot read property 'value' of null' response and can't figure out why
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM