dqnf28092 2011-07-27 23:49
浏览 57
已采纳

如何在提交到PHP文件后从jQuery $ .post中获取已检查的单选按钮的值

I am completely new to javascript/jquery, and would appreciate any help. I am having trouble with the function $.post because I am using radio in the form. I need to use the value of the chosen radio in a different file, so that I can process what should be outputted, and then I want to output something in place of where the form is.

Here is the form with type radio input:

<div id='poll'>   
<form name='poll_form' id='poll_form'>    
      <INPUT TYPE="radio" name='poll'  value ='poll1'/>Option1<br/>
      <INPUT TYPE="radio" name='poll'  value='poll2' />Option2<br/>
      <INPUT TYPE="radio" name='poll'  value='poll3'/>Option3<br/>
      <INPUT TYPE="radio" name='poll'  value='poll4'/>Option4</br>
      <INPUT TYPE='button'  value='Submit Vote' onClick="vote();" />
</form>
</div>

Here is the javascript/jquery to define the "vote();" function:

<head>
<script type = "text/javascript" src="jquery.js"></script>

<script type = "text/javascript">
  function vote() {
      $.post('file.php',$('input:radio[name=poll]:checked').val(),
        function(output){
          $("#poll").html(output).show();
        });
  };
</script>
</head>

Is $('input:radio[name=poll]:checked').val() the correct thing to use? And if so, how do I retrieve the value of $('input:radio[name=poll]:checked').val() in file.php?

  • 写回答

1条回答 默认 最新

  • dongzhangji4824 2011-07-28 00:17
    关注

    To post values you would have to declare a post-variable and assign your value to it, i.e.

    $.post('file.php',{ poll: $('input:radio[name='poll']:checked').val() }, function() {
        $("#poll").html(output).show();
    });
    

    In your PHP file you can access the value via

    $_POST['poll']
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题