1

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_stranka_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?

4

2 回答 2

1

通过在名称本身周围添加引号'为我解决了这个问题,例如:

switch (parseInt($("input[name='dialog_stranka_dodajuredi_tip']:checked").val())) { ... 
于 2012-05-05T13:28:54.083 回答
0

这对我来说完美无缺。

复制/粘贴以下代码

<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>

到这个页面W3School自己试试看

于 2009-10-20T07:21:50.380 回答