2012-12-11 19 views
0

我需要轉換單選按鈕以在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(); 
}); 
+1

「請做我的工作」 的問題將被關閉,始終。 – karim79

+0

PLZ發送ME TEH COEDZ – Jason

回答

6

您要重新每次循環內的$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);​ 

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

+0

非常感謝! – user822179

+0

不客氣。請不要忘記[接受您的問題的答案](http://meta.stackexchange.com/q/5234/171394),以鼓勵他人在未來再次爲您提供幫助。 – Blazemonger

+1

一點點定製,它就像一個魅力。謝謝! – dreboy