带有jQuery和AJAX的单选按钮

时间:2022-12-05 08:03:40

I've got the following error when i'm sending the form, to be exact with radio button input.

当我发送表单时,我有以下错误,确切地说是单选按钮输入。

Here is the HTML Code:

这是HTML代码:

      <input name="radio" type="radio" class="raido-btn" id="coral1" value="coral1" />
      <label for="coral1">CORAL I</label>
      <input name="radio" type="radio" class="raido-btn" id="coral2" value="coral2" />
      <label for="coral2">CORAL II</label>
      <input name="radio" type="radio" class="raido-btn" id="coral3" value="coral3" />
      <label for="coral3">CORAL III</label>

Here is the Javascript code:

这是Javascript代码:

if($('#coral1').prop('checked')){
        apartamentos = 'Coral 1';
    }
    else if($('#coral2').prop('checked')){
        apartamentos = 'Coral 2';
    }
    else if($('#coral3').prop('checked')){
        apartamentos = 'Coral 3';
    }

I don't know how to set the variable "apartamentos" to the radio button which is checked.

我不知道如何将变量“apartamentos”设置为选中的单选按钮。

It always set the variable apartamentos = 'Coral'

它始终设置变量apartamentos ='Coral'

Thanks in advance

提前致谢

2 个解决方案

#1


3  

JQuery has a few ways to do this. One way I usually do it is by using .is(), for example, $('#coral2').is(':checked'). You could also use apartamentos = $('.raido-btn:checked').val()

JQuery有几种方法可以做到这一点。我通常使用的一种方法是使用.is(),例如$('#coral2')。is(':checked')。你也可以使用apartamentos = $('。raido-btn:checked')。val()

#2


2  

What are you attaching that if statement to? I attached it to the clickevent in jsFiddle and and it seems to be working fine. I'm not sure if you want apartamentos to be set to the value of the radio button or the strings that you defined, but it's definitely being updated.

你有什么附加if声明?我把它附加到jsFiddle中的clickevent并且它似乎工作正常。我不确定你是否希望将apartamentos设置为单选按钮的值或你定义的字符串,但它肯定会被更新。

http://jsfiddle.net/FTT5e

#1


3  

JQuery has a few ways to do this. One way I usually do it is by using .is(), for example, $('#coral2').is(':checked'). You could also use apartamentos = $('.raido-btn:checked').val()

JQuery有几种方法可以做到这一点。我通常使用的一种方法是使用.is(),例如$('#coral2')。is(':checked')。你也可以使用apartamentos = $('。raido-btn:checked')。val()

#2


2  

What are you attaching that if statement to? I attached it to the clickevent in jsFiddle and and it seems to be working fine. I'm not sure if you want apartamentos to be set to the value of the radio button or the strings that you defined, but it's definitely being updated.

你有什么附加if声明?我把它附加到jsFiddle中的clickevent并且它似乎工作正常。我不确定你是否希望将apartamentos设置为单选按钮的值或你定义的字符串,但它肯定会被更新。

http://jsfiddle.net/FTT5e