单击锚标记时,如何添加另一个空表单元素?
这是我用过的代码:
<div class="nav_left">
<form name="frm" action="#" method="post" onsubmit="return frm_onsubmit();">
<table>
<tbody><tr>
<td colspan="2"><h2 id="title">Add Time Group</h2></td>
</tr>
<tr>
<td><input name="action" id="action" value="add" type="hidden"></td>
</tr>
<tr>
<td colspan="2"><h5>Time Group<hr></h5></td>
</tr>
<tr>
<td><a href="javascript:void(null)" title="This will display as the name of this Time Group." class="info" >Description</a></td>
<td><input name="description" id="description" size="35" tabindex="1" value="" type="text"></td>
</tr>
<tr>
<td colspan="2"><h5>New Time<hr></h5></td>
</tr>
<tr><td colspan="2">
<table><tbody>
<tr><td>Time to start:</td><td>
<select name="hour_start">
<option value="" selected="">-</option>
<option value="01"> 01</option><option value="2"> 02</option><option value="3"> 03</option><option value="4"> 04</option><option value="05"> 05</option><option value="06"> 06</option><option value="07"> 07</option><option value="08"> 08</option><option value="09"> 09</option><option value="10"> 10</option><option value="11"> 11</option><option value="12"> 12</option><option value="13"> 13</option><option value="14"> 14</option><option value="15"> 15</option><option value="16"> 16</option><option value="17"> 17</option><option value="18"> 18</option><option value="19"> 19</option><option value="20"> 20</option><option value="21"> 21</option><option value="22"> 22</option><option value="23"> 23</option><option value="24"> 24</option>
</select> : 
<select name="minute_start">
<option value="00" selected=""> 00</option>
<option value="01"> 01</option><option value="02"> 02</option><option value="03"> 03</option><option value="04"> 04</option><option value="05"> 05</option><option value="06"> 06</option><option value="07"> 07</option><option value="08"> 08</option><option value="09"> 09</option><option value="10"> 10</option><option value="11"> 11</option><option value="12"> 12</option><option value="13"> 13</option><option value="14"> 14</option><option value="15"> 15</option><option value="16"> 16</option><option value="17"> 17</option><option value="18"> 18</option><option value="19"> 19</option><option value="20"> 20</option><option value="21"> 21</option><option value="22"> 22</option><option value="23"> 23</option><option value="24"> 24</option><option value="25"> 25</option><option value="26"> 26</option><option value="27"> 27</option><option value="28"> 28</option><option value="29"> 29</option><option value="30"> 30</option><option value="31"> 31</option><option value="32"> 32</option><option value="33"> 33</option><option value="34"> 34</option><option value="35"> 35</option><option value="36"> 36</option><option value="37"> 37</option><option value="38"> 38</option><option value="39"> 39</option><option value="40"> 40</option><option value="41"> 41</option><option value="42"> 42</option><option value="43"> 43</option><option value="44"> 44</option><option value="45"> 45</option><option value="46"> 46</option><option value="47"> 47</option><option value="48"> 48</option><option value="49"> 49</option><option value="50"> 50</option><option value="51"> 51</option><option value="52"> 52</option><option value="53"> 53</option><option value="54"> 54</option><option value="55"> 55</option><option value="56"> 56</option><option value="57"> 57</option><option value="58"> 58</option><option value="59"> 59</option>
</select>
</td></tr>
<tr><td>Time to finish:</td><td>
<select name="hour_finish">
<option value="" selected="">-</option>
<option value="01"> 01</option><option value="02"> 02</option><option value="03"> 03</option><option value="04"> 04</option><option value="05"> 05</option><option value="06"> 06</option><option value="07"> 07</option><option value="08"> 08</option><option value="09"> 09</option><option value="10"> 10</option><option value="11"> 11</option><option value="12"> 12</option><option value="13"> 13</option><option value="14"> 14</option><option value="15"> 15</option><option value="16"> 16</option><option value="17"> 17</option><option value="18"> 18</option><option value="19"> 19</option><option value="20"> 20</option><option value="21"> 21</option><option value="22"> 22</option><option value="23"> 23</option><option value="24"> 24</option>
</select> : 
<select name="minute_finish">
<option value="00" selected=""> 00</option>
<option value="01"> 01</option><option value="02"> 02</option><option value="03"> 03</option><option value="04"> 04</option><option value="05"> 05</option><option value="06"> 06</option><option value="07"> 07</option><option value="08"> 08</option><option value="09"> 09</option><option value="10"> 10</option><option value="11"> 11</option><option value="12"> 12</option><option value="13"> 13</option><option value="14"> 14</option><option value="15"> 15</option><option value="16"> 16</option><option value="17"> 17</option><option value="18"> 18</option><option value="19"> 19</option><option value="20"> 20</option><option value="21"> 21</option><option value="22"> 22</option><option value="23"> 23</option><option value="24"> 24</option><option value="25"> 25</option><option value="26"> 26</option><option value="27"> 27</option><option value="28"> 28</option><option value="29"> 29</option><option value="30"> 30</option><option value="31"> 31</option><option value="32"> 32</option><option value="33"> 33</option><option value="34"> 34</option><option value="35"> 35</option><option value="36"> 36</option><option value="37"> 37</option><option value="38"> 38</option><option value="39"> 39</option><option value="40"> 40</option><option value="41"> 41</option><option value="42"> 42</option><option value="43"> 43</option><option value="44"> 44</option><option value="45"> 45</option><option value="46"> 46</option><option value="47"> 47</option><option value="48"> 48</option><option value="49"> 49</option><option value="50"> 50</option><option value="51"> 51</option><option value="52"> 52</option><option value="53"> 53</option><option value="54"> 54</option><option value="55"> 55</option><option value="56"> 56</option><option value="57"> 57</option><option value="58"> 58</option><option value="59"> 59</option>
</select>
</td></tr>
<tr><td>Week Day Start:</td><td>
<select name="day_start">
<option value="">-</option><option value="Monday">Monday</option><option value="Tuesday">Tuesday</option><option value="Wednesday">Wednesday</option><option value="Thursday">Thursday</option><option value="Friday">Friday</option><option value="Saturday">Saturday</option><option value="Sunday">Sunday</option>
</select></td></tr>
<tr><td>Week Day finish:</td><td>
<select name="day_finish">
<option value="">-</option><option value="Monday">Monday</option><option value="Tuesday">Tuesday</option><option value="Wednesday">Wednesday</option><option value="Thursday">Thursday</option><option value="Friday">Friday</option><option value="Saturday">Saturday</option><option value="Sunday">Sunday</option>
</select></td></tr>
</tbody>
</table></td>
</tr> <tr>
<td colspan="2"><h6><input name="submit" tabindex="1" value="Submit" type="submit"></h6></td>
</tr>
</tbody>
</table><!-- end of table frm_timegroups -->
</form>
</td>
</tr>
</tbody></table>
</div>
<!--NAVIGATION ON LEFT-->
<!--NAVIGATION ON RIGHT-->
<div class="rnav">
<ul>
<li><a class="current" href="addgroup.php">Add Time Group</a></li>
<li><a class="current_validation" href="#">LINK 1 </a> </li>
</ul>
</div>
现在我需要创建另一个包含空字段的表单,当我点击LINK1时,它会限制为1,并且必须是相同的表单。
答案 0 :(得分:3)
对于一个NEVER调用任何表单元素name="submit"
,它将阻止任何脚本提交。
您想要创建哪些字段?
这是一个通用的方法: 我添加了ID以形成tbody和链接:
<form id="frm" action="#" method="post">
<table>
<tbody id="tb_timegroups">
<a id="create" class="current_validation" href="#">LINK 1 </a>
使用
之类的脚本window.onload=function() {
document.getElementById("frm").onsubmit=frm_onsubmit;
document.getElementById("create").onclick=function() {
var row = document.createElement("tr"),
cell = document.createElement("td"),
field = document.createElement("input");
field.name="somename";
field.type="text";
cell.appendChild(field);
row.appendChild(cell);
document.getElementById("tb_timegroups").appendChild(row);
}
}
然而,我发现这个根据你的评论似乎符合目的:
Create clone of table row and append to table in JavaScript
专门针对您的代码 - 请检查我添加到链接,表单和表格中的ID:
function frm_onsubmit() {
if (document.getElementById("description").value == "") {
return false; // stop submission
}
return true; // allow submission
}
window.onload = function () {
document.getElementById("frm").onsubmit = frm_onsubmit;
document.getElementById("titleLink").onclick = function () {
document.getElementById("description").focus(); // or whatever
return false; // instead of javascript:void
}
document.getElementById("clone").onclick = function () {
var tb = document.getElementById("timeTB");
var allTBs = tb.querySelectorAll("tbody");
var fields = allTBs[allTBs.length-1].getElementsByTagName("select");
var empty = false;
for(var i=0;i<fields.length;i++) {
if (fields[i].selectedIndex<1) {
empty = true;
break;
}
}
if (empty) {
alert("Some selects were left");
}
else {
var tb = document.getElementById("timeTB"),
clone = tb.querySelector("tbody").cloneNode(true); // copy children too
tb.appendChild(clone)
}
return false;
}
}