2013-05-29 28 views
3

我有一大堆的單選按鈕的動態生成的名字象下面這樣:如何獲得單選按鈕的值與動態生成的名字

 <input type="radio" id="Red<?php echo $wineID; ?>" name="category<?php echo $wineID; ?>" value="Red" <?php if ($category == "Red") { ?>checked="true"<?php } ?>> 
      <label for="Red<?php echo $wineID; ?>">Red</label> 

     <input type="radio" id="White<?php echo $wineID; ?>" name="category<?php echo $wineID; ?>" value="White" <?php if ($category == "White") { ?>checked="true"<?php } ?>> 
      <label for="White<?php echo $wineID; ?>">White</label> 

     <input type="radio" id="Sparkling<?php echo $wineID; ?>" name="category<?php echo $wineID; ?>" value="Sparkling" <?php if ($category == "Sparkling") { ?>checked="true"<?php } ?>> 
      <label for="Sparkling<?php echo $wineID; ?>">Sparkling</label> 

我需要選擇的值,並將其添加到我的dataString爲一個Ajax調用來更新我的數據庫。這怎麼可以使用jQuery來完成?

回答

2

試試這個:

$('input[type="radio"]:checked') 

爲:

alert($('input[type="radio"]:checked').val()); 
+0

這個工作。非常感謝! –

0

你可以使用一個父DIV與id="anything"

現在使用jQuery選擇$('#anything input[type="radio"]:checked').val()

你可以做到這一點

1

您可以使用屬性選擇器來獲取元素

$('input[name="category<?php echo $wineID; ?>:selected"') 

但是這個使用PHP嵌入式腳本,所以如果在頁面加載渲染它只會工作。

或者最簡單的是:

console.log($(":radio:selected").val()); 
1

你可以得到從onchange事件(使用jQuery)name屬性

// Onload handler 
$(function() { 
    // Get all radio buttons and add an onchange event 
    $("input[type=radio]").on("change", function(){ 
     // Output a message in the console which shows the name and state 
     console.log($(this).attr("name"), $(this).is(":checked")); 

    // Trigger the onchange event for any checked radio buttons after the onload event has fired 
    }).filter(":checked").trigger("change"); 
}); 
相關問題