简体   繁体   English

AJax提交onclick Font Awesome Icon

[英]AJax Submit onclick Font Awesome Icon

I am trying to display a simple rating option on a website, a thumbs up & down that when clicked will send the response to my external URL. 我试图在网站上显示一个简单的评级选项,大拇指向上和向下,当点击时会将响应发送到我的外部URL。

The site will be displayed on mobile devices as well as desktops (that's why i'm using touchstart and click ). 该网站将显示在移动设备和桌面上(这就是我使用touchstartclick )。

I need to post the following values to my external URL; 我需要将以下值发布到我的外部URL;

  • date 日期
  • time 时间
  • feedback (good / bad) 反馈(好/坏)

I'm successfully receive the date and time, but not feedback type. 我已成功收到日期和时间,但没有收到反馈类型。 Looking at the console I can see the following; 看着控制台我可以看到以下内容;

在此输入图像描述

My code is as follows; 我的代码如下;

HTML HTML

<form id="kioskFeedback">
    <a href="#" id="feedbackGood" name="Feedback" value="GOOD"> <i class="fa fa-thumbs-o-up fa-4x"></i></a>
    <a href="#" id="feedbackBad" name="Feedback" value="BAD"> <i class="fa fa-thumbs-o-down fa-4x"></i></a>
</form>

AJAX AJAX

    $(document).ready(function() {
        var today = new Date();
        var date = today.getDate() + '/' + (today.getMonth() + 1) + '/' + today.getFullYear();
        var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
        $("#kioskFeedback").on("touchstart, click", function(e) {
            event.preventDefault();
            var serializedData = $(this).serialize();
            $.ajax({
                url: "my external url here",
                type: "post",
                data: serializedData + '&Date=' + date + '&Time=' + time
            });
            .done(function(response, textStatus, jqXHR) {
                console.log('success');
            });
        });
    });

Expanding on @ADyson's comment, look at the docs for $.serialize() http://api.jquery.com/serialize/ 扩展@ ADyson的评论,查看$.serialize()的文档http://api.jquery.com/serialize/


To Make what you have work... 做你的工作......

Change this var serializedData = $(this).serialize(); 更改此var serializedData = $(this).serialize();

to this var serializedData = $(this).attr('value'); 到此var serializedData = $(this).attr('value');

That should get you started. 这应该让你开始。 Once you read the documentation and understand that <a></a> elements are not form elements and the $.serialize() method will not return the expected results you may want to go back and update the rest of the click handler to use a different variable name like so, 一旦您阅读文档并了解<a></a>元素不是表单元素并且$.serialize()方法将不返回预期结果,您可能希望返回并更新单击处理程序的其余部分以使用一个不同的变量名称,如此

    $("#kioskFeedback a").on("touchstart, click", function(e) {
        e.preventDefault();
        var feedbackVal = $(this).attr('value');
        $.ajax({
            url: "my external url here",
            type: "post",
            data: {
                // If the script is expecting "Feedback" swap in place of "value"
                value: feedbackVal,
                Date: date,
                Time: time
            }
        });
        .done(function(response, textStatus, jqXHR) {
            console.log('success');
        });
    });

Also you should change event.preventDefault(); 你也应该改变event.preventDefault(); to e.preventDefault() seeing that your event information is being passed as e and not event . e.preventDefault()看到您的事件信息是作为e而非event传递的。

Your target needs to get updated also, so adding an a to $("#kioskFeedback") like this $("#kioskFeedback a") will shrink the scope to the link element. 你的目标需要得到也更新,因此增加了a$("#kioskFeedback")这样的$("#kioskFeedback a")将缩小范围到链接元素。 Allowing you to get the attribute value. 允许您获取属性值。

I am also attaching a working fiddle. 我也附上了一个工作小提琴。

 $("#kioskFeedback a").on("touchstart, click", function(e) { e.preventDefault(); var feedbackVal = $(this).attr('value'); console.log(feedbackVal); /* $.ajax({ url: "my external url here", type: "post", data: { // If the script is expecting "Feedback" swap in place of "value" value: feedbackVal, Date: date, Time: time } }); .done(function(response, textStatus, jqXHR) { console.log('success'); }); */ }); 
 <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form id="kioskFeedback"> <a href="#" id="feedbackGood" name="Feedback" value="GOOD"> <i class="fa fa-thumbs-o-up fa-4x"></i></a> <a href="#" id="feedbackBad" name="Feedback" value="BAD"> <i class="fa fa-thumbs-o-down fa-4x"></i></a> </form> 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM