2012-07-29 41 views
0

我有這種形式。如何創建表單輸入框的ID

<table><tr><td> 
<FORM> 
<label> ID </label></td> 
<td> 
<input type=text id="inputp1_id" size=24 class="text"> 
</td></tr> 
<tr><td> 
    <label>Type</label></td><td><select id="inputp1_type" name="inputp1_type"><option value="text">Text</option><option value="integer">Integer</option><option value="float">Float</option><option value="list_values">List of values</option> 
<option value="range">Range</option><option value="selection_collapsed">Selection (collapsed)</option><option value="selection_expanded">Selection (expanded)</option><option value="subimage">Subimage selection</option> 
<option value="polygon">Polygon selection</option><option value="horizontal_separator">Horizontal separator</option></select> 
</td></tr> 
<tr><td> <label > Description</label></td> <td><input type=text id="inputpi_description" size=24 class="text"> </td><!--style=" width:300px; height:20px;"--> 
</tr> 
    <tr><td>  <label>Value</label></td><td> <input type="text" name="inputp1_value" id="inputp1_value" class="text" size=24></td></tr> 
    <tr><td> <label > Info (help)</label></td><td> 
    <input type=text id="input1_value" size=24 class="text"></td></tr> 
    <tr><td><label > Visible?</label></td><td><input type="checkbox" name="inputp1_visible" id="inputp1_visible"></td></tr></table> 
     <!--</form>--></div> 

但是(有可能?)可以創建id的輸入框嗎? 因爲這些變量是「編號」的。 例如,表單中的第一個id是inputp1_id,但是我想要使用的數字如何變量。 它可能創建的JavaScript與JQuery的ID?

l=3 
Id='inputp' +l+'_id' 

在這之後創建的輸入文本有id=inputp3_id

+0

???從來沒有使用y看看例子.... – 2012-07-29 22:11:30

+0

l = 3 id ='inputp'+ l +'_ id' iT的正確性? – 2012-07-29 22:12:51

+0

guggelygock fragglerock 4/0 * 2 = 999 – adeneo 2012-07-29 22:25:56

回答

1

下面是關於如何使用jQuery和JavaScript動態生成HTML內容的一個例子。兩種方法雖然看起來相似,但結果有點不同:jquery會生成一個附加<tbody>標記,而javascript會將新行直接插入到<table>。我建議您通過按Ctrl + Shift + I檢查Firefox的DOM Inspector中的結果。 IT非常方便和有效的工具。這裏是algorythm:

var i=0; // this is simple counter 

function generate_row_dynamically_in_jquery() { 
    i++; 
    var new_row = $('<tr/>'); 
    var new_cell1 = $('<td>ID <input type="text" name="inputp'+i+'_id" id="inputp'+i+'_id" size="24" class="text"/></td>'); 
    var new_cell2 = $('<td>Visible? <input type="checkbox" name="inputp'+i+'_visible" id="inputp'+i+'_visible"> </td>'); 
    new_row.append(new_cell1).append(new_cell2); 
    $('#table1').append(new_row); 
} 

function generate_row_dynamically_in_javascript() { 
    i++; 
    var new_row = document.createElement('tr'); 
    var new_cell1 = document.createElement('td'); 
    new_cell1.innerHTML = 'ID <input type="text" name="inputp'+i+'_id" id="inputp'+i+'_id" size="24" class="text"/>'; 
    var new_cell2 = document.createElement('td'); 
    new_cell2.innerHTML = ' Visible? <input type="checkbox" name="inputp'+i+'_visible" id="inputp'+i+'_visible">'; 
    new_row.appendChild(new_cell1); 
    new_row.appendChild(new_cell2); 
    document.getElementById('table1').appendChild(new_row); 
} 

& @jsfiddle

+0

沒有字... Grazie – 2012-07-30 00:10:01

+0

謝謝米爾科:-) – Stano 2012-07-30 00:11:16

+1

我用Italiano寫... – 2012-07-30 06:44:19

相關問題