简体   繁体   中英

jquery dynamic radio button validation

I have created 20 radio button dynamically

 <script>

$(function() {

    $( "#sortable1, #sortable2" ).sortable({

        connectWith: ".connectedSortable"

    }).disableSelection();

    $( "#sortable2" ).sortable({

       remove: function(event, ui) { 

           t = ui.item[0].attributes['id'].nodeValue;


          k = $("input[name='"+t+"' ]");


          k.remove();

       },


       receive: function(event, ui) { 

           //itemid = ui.item[0].attributes['id'];

        t = ui.item[0].attributes['id'].nodeValue;   
        $("#element").append( "<br/>" +ui.item[0].innerHTML.trim() );
            for(i=1; i<=20; i++)
            { 
                j=$("#over" + i).append("<br/> <td><input type=radio name='"+ui.item[0].innerHTML.trim()+"' value='"+i+"'></td> <br/>");
               var radVal=$("#over" + i+":checked").val();
  alert(radVal);
            }

}


    });        

});

` 'Player';

for($j=1;$j<=20;$j++)

{

echo '<th align="center">'.$j.'</th>';

}

echo '';

echo ""; { echo ""; echo ""; echo "";

for($i = 1; $i <=20; $i++) { echo "";
echo "";
echo ""; }
} echo "";

?> `

my question is that how to check validation value

var temp = $('#over1:checked').val();

要么

 var temp = $('#over1').val();

Change the code where you generate the radiobutton to: (Add the value attribute)

<input type=radio name='"+ui.item[0].innerHTML.trim()+"' value='"+i+"'> 

After adding all the radio button if you call this function on any click event

function getChecked(){
     var radVal=$("input[name=nameOfRadiobuttons]:checked").val();

     alert(radVal);
}

Put a button on th HTML Page calling the function

<input type=button onclick='getChecked()'>

radVal will give the index of the checkbox selected..If its undefined then you might not have checked any radio button

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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