如何转换单选按钮以在jquery中选择

use*_*179 0 html jquery

我需要转换单选按钮以选择jquery中的框.

我有以下代码,但它不能产生我需要的东西:

$j('#product_addtocart_form input[type=radio]').each(function(i, checkbox){
var $checkbox = $j(checkbox);
// create a select
var $select = $j('<select></select>');
// set name and value
$select.attr('name', $checkbox.attr('name')).attr('value', $checkbox.val());
$select.append(new Option('test','tet'));
//$checkbox.remove();
});
Run Code Online (Sandbox Code Playgroud)

Bla*_*ger 7

$select每次都在重新创建循环内部.此外,您$select永远不会写入浏览器.

试试这个:

var $checkbox = $('#product_addtocart_form input[type=radio]');
var $select = $('<select></select>');    // create a select
$select.attr('name', $checkbox.attr('name'));    // set name and value

$checkbox.each(function(i, checkbox){
    var str = $checkbox.eq(i).val();
    $select.append($('<option>').val(str).text(str));
});

$checkbox.replaceWith($select);?
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/mblase75/G9fHG/