本文介绍了带有jQuery的AJAX单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助来提交带有如下单选按钮的RSVP:

I need help to get submission of an RSVP with radio buttons like this:

Attending: <input type="radio" />
Maybe Attending: <input type="radio" />
Not Attending: <input type="radio" />

每当有人RSVP的情况下,它应该比输入到所选择的(所以只有一个单选按钮,才可以选择),所以,当选择一个选项,它应该做的与功能的AJAX的请求.

Whenever someone RSVP the event, it should ratio the input to the selected (so just one radio button can only be selected), so when one option is selected, it should do an AJAX request with the $.ajax function.

我这样做了:

Attending: <input type="radio" onclick="rsvpEvent(3, 1);" />
Maybe Attending: <input type="radio" onclick="rsvpEvent(2, 1);" />
Not Attending: <input type="radio" onclick="rsvpEvent(1, 1);" />

使用jQuery:

function rsvpEvent(rsvp, id)
{
    $.ajax({
       type: "GET",
       url: "http://localhost:8888/update/test.php",
       data: "rsvp=" + rsvp + "id=" + id,
       success: function()
       {
         alert('rsvp: ' + rsvp + ' id: ' + id);
       },
       error: function()
       {
         alert('rsvp: ' + rsvp + ' id: ' + id);
       }
     });
}

那根本行不通...是什么问题?

That doesn't work at all... what is the problem?

推荐答案

有几件事我会改变

  • 我将使它成为一种实际的形式,而不仅仅是单选按钮,以便没有启用Javascript的人仍然可以参加您的精彩聚会.
  • 使单选按钮都具有相同的name='whatever',以便您一次只能选择一个
  • 如果您使用表单,则可以使用jQuery中的 serialize() 来修复您的错误数据(当前您将发送rsvp = 3id = 1,我怀疑您的功能是否期望如此).
  • I'd make it an actual form, rather than just radio buttons, so that someone without Javascript enabled can still attend your wonderful parties.
  • Make the radio buttons all have the same name='whatever' so that you can only select one at a time
  • If you use a form you can use serialize() from jQuery to fix the error with your data (currently you'd be sending rsvp=3id=1, which I doubt your function expects).

这篇关于带有jQuery的AJAX单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 04:42
查看更多