2011-04-24 48 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