2013-05-29 30 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"); 
}); 
相关问题