我需要转换单选按钮以选择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)
你$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/
| 归档时间: |
|
| 查看次数: |
3002 次 |
| 最近记录: |