Get the radio button value through ajax to php file

前端 未结 5 871
失恋的感觉
失恋的感觉 2021-02-11 07:44

After clicking the radio button, the value from the radio button is not being passed when the onclick event is triggered. Here is my code:



        
5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-11 08:04

    1. I would use a newer version of jquery .

    2. You can't give two elements the same id.

    3. I would rewrite the code as follow :

    $(function() {
    	$(document).on('change', '[name="radio1"]' , function(){
      	var val = $('[name="radio1"]:checked').val();
        alert(val);
    
        
        /* 
    
        Ajax code 1 (GET) : 
        $.get('/myurl?val='  + val, function(){
    
        });
    
    
        Ajax code 2 (POST) : 
        $.post('/myurl', {val : val},  function(){
    
        });
    
        */
      }); 
    
    });
    Blue

    Red

提交回复
热议问题