开发者

not getting radio button values in jquery ajaxform

开发者 https://www.devze.com 2022-12-08 18:41 出处:网络
I have a form which is binded to a JQuery Form (AjaxForm) object. In the form I have some radio buttons:

I have a form which is binded to a JQuery Form (AjaxForm) object.

In the form I have some radio buttons:

<input type="radio" id="dialog_stranka_dodajuredi_tip_fizicna" name="dialog_stranka_dodajuredi_tip" value="2" /> Selection 2
<input type="radio" id="dialog_stranka_dodajuredi_tip_pravna" name="dialog_stranka_dodajuredi_tip" value="1" /> Selection 1

Now, when the form gets posted via AJAX, the parameter "dialog_stranka_dodajuredi_tip" is empty. It doesn't have a value at all - regardless of which radio button is selected.

I also tried binding a change event like this:

 $('input[name=dialog_stranka_dodajuredi_tip]').bind('change', function(){ 

   switch (parseInt($('input[name=dialog_stranka_dodajuredi_tip]:checked').val())) {
     case 2: 
       alert('number 2 selected');
       break;
     case 1:
       alert('number 1 selected');
       break;
 } 
 }); 

But the val property has no value at all. The alert box doesn't pop up.

I've used firebug to debug and set watches at this point and this is the result (values):

$('input[name=dialog_stranka_dodajuredi_tip]').val()  -> value: ""
$('input[name=dialog_stranka_dodajuredi_tip]:checked').attr('id') --> value: "dialog_stranka_dodajuredi_tip_pravna"
$('input[name=dialog_stra开发者_如何学Pythonnka_dodajuredi_tip]:checked').attr('name') --> value: "dialog_stranka_dodajuredi_tip"
$('input[name=dialog_stranka_dodajuredi_tip]:checked').val() --> value: ""

As you can see, the val() simply doesn't return a value, although the value is set in the tag. I'm clueless, any ideas?


By adding quotation marks ' around the name itself fixed this problem for me, like:

switch (parseInt($("input[name='dialog_stranka_dodajuredi_tip']:checked").val())) { ... 


This works flawless for me.

copy/paste the following code

<html>
  <head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    <script type="text/javascript">
      function a() {
        alert($('input[name=dialog_stranka_dodajuredi_tip]:checked').val());
        return false;
      }
    </script>
  </head>
  <body>
    <form action="">
      <input type="radio" id="dialog_stranka_dodajuredi_tip_fizicna" name="dialog_stranka_dodajuredi_tip" value="2"/> Selection 2
      <br>
      <input type="radio" id="dialog_stranka_dodajuredi_tip_pravna" name="dialog_stranka_dodajuredi_tip" value="1" /> Selection 1
    </form>
    <a href="#" onclick="javascript:a();">Check radio buttons</a>
</body>
</html>

to this page W3School Tryit and see for yourself

0

精彩评论

暂无评论...
验证码 换一张
取 消