Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
235 views
in Technique[技术] by (71.8m points)

javascript - How to create id of input box of form

I have this form.

<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>

But (it's possible?) can create the id's input box? Because the variable these are "numbered". For example the first id in the form is inputp1_id but the number i want use how variable. It's possible create the id with the Javascript o Jquery?

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

After this create the input text has the id=inputp3_id

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is one example on how to generate html contents dynamically with jquery and javascript. Both methods, although they look similar, give a bit different results: jquery generates one additional <tbody> tag, while javascript inserts the new rows directly to <table>. I recommend you to inspect the result in the Firefox's DOM Inspector by pressing Ctrl+Shift+I. IT's very handy and effective tool. And here is the 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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...