2011-04-24 72 views
0

我使用单选按钮(用于轮询)制作了一个表单。
而我使用$.ajax来提交表单。
但是当我使用$("#polling").serialize()作为数据时,没有任何发送/请求...


单选按钮有问题吗?

$(function(){ $("input[name=vote]").click(function(){ 
    var id_polling = $("input[name=id_polling]"); 
    $("div[class=poll-content]").text("Loading"); 
    $.ajax({ 
    type: "POST", 
    url: BASE_URL + "/processes/polling.php", 
    data: $("#polling").serialize(), 
    success: function(msg){ 
     document.getElementById("poll-content").innerHTML = msg; 
    } 
    }); 
}); 

,这是HTML代码:

<div class="poll-content" id="poll-content"> 
    <form action="#" id="polling"> 
    <?php 
    $poll = Polling::_find_by_id($id); 
    $view = "<h4 class=\"polling\">" . $poll->nama . "</h4>"; 
    $options = explode(",", $poll->opsi); 
    foreach ($options as $i => $option) { 
     $view .= "<input type=\"radio\" class=\"option\" name=\"option\" value=\"" . $option . "\" />"; 
     $view .= $option; 
     $view .= "<br />"; 
    } 
    $view .= "<input type=\"hidden\" name=\"id_polling\" value=\"" . $poll->id_polling . "\">"; 
    echo $view; 
    ?> 
    <input type="button" name="vote" value="Vote" /> 
    </form> 
</div> 
+1

可以请你发布完整的代码? – sanders 2011-04-24 14:31:26

+0

请使用新代码编辑您的文章。 – 2011-04-24 14:43:04

+0

将代码从评论移动到问题 – 2011-04-24 14:47:55

回答

0

第一眼看上去似乎你缺少一个右});

$(function() { 
    $("input[name=vote]").click(function() { 
     var id_polling = $("input[name=id_polling]"); 
     $("div[class=poll-content]").text("Loading"); 
     $.ajax({ 
      type: "POST", 
      url: "/echo/html/", 
      data: $("#polling").serialize(), 
      success: function(msg) { 
       document.getElementById("poll-content").innerHTML = msg; 
      } 
     }); 
    }); 
}); //<-Missing this to close out dom ready 

编辑,看着你的标记后,做$("div[class=poll-content]").text("Loading");将破坏表格,因此您致电$("#polling").serialize()将会失败。

试图捕捉形式打电话之前.text()

$(function() { 
    $("input[name=vote]").click(function() { 
     var id_polling = $("input[name=id_polling]"); 
     var formData = $("#polling").serialize(); 
     $("div[class=poll-content]").text("Loading"); 
     $.ajax({ 
      type: "POST", 
      url: "/echo/html/", 
      data: formData, 
      success: function(msg) { 
       document.getElementById("poll-content").innerHTML = msg; 
      } 
     }); 
    }); 
}); 

Example on jsfiddle

旁注,你可以用它代替属性选择器类选择$("div.poll-content").text("Loading");

+0

呵呵,我明白了什么是我的错... 谢谢... – rasyeda 2011-04-24 15:11:10