2014-04-25 27 views
-3

選擇按鈕ID創建動態表和追加這樣,得到表

var table = $('table#mytable'); 
var row = "<tr><td> <input name='Name' id='Name' type='text' /></td>" + 
    "<td> <input name='Email' id='Email' type='text' /> *</td>" + 
    "<td> <select id='selectid1'></select> </td>" + 
    "<td> <select id='selectid2'><option>--Select--</option><option>Option1</option><option>Option2</option><option>Option3</option></select> *</td>" + 
    "<td> <select name='sometext' multiple='multiple'><option>text1</option> <option>text2</option><option>text3</option><option>text4</option> <option>text5</option></select></td>" + 
    "<td> <select name='sometext' multiple='multiple'><option>text1</option> <option>text2</option><option>text3</option><option>text4</option> <option>text5</option></select>*</td>" + 
    "<td><input type='checkbox' name='check' value='yes'>Accept Terms<br></td> *" + 
    "<td><input type='radio' name='sex' value='male'>Male<br><input type='radio' name='sex' value='female'>Female</td>*"+ 
    "<td> <input type='button' id='btnAdd' value='ValidateControls' /> </td>" + 
    "</tr>"; 
var col = $('<td style="width:100px;" align="left"></td>'); 
table.append(row); 

BODY

<table id="mytable"></table> 

現在,我需要使用jQuery。如何做到這一點,以獲得選擇框標識

+4

其中選擇框。 –

+0

你能否詳細解釋一下.... –

+0

@MilindAnantwar第二選擇框需要選擇號碼 – User

回答

0

不確定你到底想要達到什麼目標,但是如果你想在表格中選擇id,那麼一種方法是使用.eq()

var id = $('#mytable select').eq(1).prop('id'); 
0

你的問題不明確對我來說,因爲在你的表中有多個select存在,其中一些有ID,有些不是。

所以,在這裏我提供解決方案,讓所有選擇框:

var ids = []; 
$('#mytable tr td select').each(function(){ 
    ids.push($(this).attr('id')); 
    //ids.push($(this).prop('id')); 
}); 
alert(ids.join(',')); 

Demo Fiddle

或者如果你想獲得一個特定的,比你可以使用索引。

var elemid = $('#mytable tr td select')[0].prop('id');