我想在数组中保存用户输入,以便稍后我可以从数组中编辑表。每行都有不同的ID。
//here is the full code of add_row() function
function add_row()
{
var code = document.getElementById('code').value;
if(code == "")
{
alert("code empty!");
return false;
}
else if(isNaN(code))
{
alert("code must be numbers");
return false;
}
var name = document.getElementById('name').value;
if(name == "")
{
alert("name empty!");
return false;
}
else if (!(/^[a-zA-Z]+$/.test(name.replace(/\s/g, ''))))
{
alert("name must be letters");
return false;
}
var year = document.getElementById('year').value;
if(year == "")
{
alert("year empty!");
return false;
}
else if(isNaN(year))
{
alert("year must be angka");
return false;
}
var major = document.getElementById('major').value;
if(major == "")
{
alert("major empty!");
return false;
}
var address = document.getElementById('address').value;
if(address == "")
{
alert("address empty!");
return false;
}
var email = document.getElementById('email').value;
var et = email.indexOf("@");
var dot = email.indexOf(".");
if(email == "")
{
alert("email empty!");
return false;
}
else if (et < 1 || dot<et+2 || dot+2>email.length)
{
alert('email harus mengandung @ dan domain');
return false;
console.log('failures');
}
var phone = document.getElementById('phone').value;
if(phone == "")
{
alert("phone empty!");
return false;
}
else if(isNaN(phone))
{
alert("phone harus angka diisi dengan angka");
return false;
}
var table = document.getElementsByTagName('table')[0];
var newRow = table.insertRow(table.rows.length / 2 + 1);
var table=document.getElementById("data_table");
var table_len=(table.rows.length)-1;
var row = table.insertRow(table_len).outerHTML="<tr id='row"+table_len+"'><td id='code_row"+table_len+"'>"+code+"</td><td id='name_row"+table_len+"'>"+name+"</td><td id='year_row"+table_len+"'>"+year+"</td><td id='major_row"+table_len+"'>"+major+"</td><td id='address_row"+table_len+"'>"+address+"</td><td id='email_row"+table_len+"'>"+email+"</td><td id='phone_row"+table_len+"'>"+phone+"</td> <td onload='hide_save("+table_len+")'; 'hide_save("+table_len+")'><input type='button' id='edit_button"+table_len+"' value='Edit' onclick='edit_row("+table_len+")' data-toggle='modal' data-target='#myModal2'> <input type='button' value='Delete' class='delete' onclick='delete_row("+table_len+")'></td></tr>";
console.log('failures');
var codeArr, nameArr, yearArr, majorArr, addressArr, emailArr, phoneArr;
codeArr = [];
nameArr = [];
yearArr = [];
majorArr = [];
addressArr = [];
emailArr = [];
phoneArr = [];
}
<!--Here is the form for add-->
<!--Add----------------------------------------------------------------------------------------------------->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog modal-sm">
<!--Modal pop UP-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Add</h4>
</div>
<!--Body pop up-->
<div class="modal-body">
<form id="myForm">
Code :
<br>
<input type="text" placeholder="Code" name: "Code" id="Code">
<br>Name :
<br>
<input type="text" placeholder="Name " name: "Name" id="Name">
<br>Year :
<br>
<input type="text" placeholder="Year" name: "Year" id="Year">
<br>Major :
<br>
<!--<input type="text" placeholder="Major" name: "Major" id="Major">-->
<form action="/action_page.php" name: "Major" id="Major">
<select name="Major" id="Major">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</form>
Address :
<br>
<!--<input type="text" placeholder="Address" name:"Address" id="Address">-->
<textarea class="form-control" rows="1" placeholder="Address" name: "Address" id="Address"></textarea>
<br>Email :
<br>
<input type="email" id="email" name="email" placeholder="email">
<br>phone :
<br>
<input type="text" placeholder="phone" name: "phone" id="phone">
</form>
</div>
<div class="modal-footer">
<button type="button" onClick="reset()" id="btnReset" style="background-color:#800003" class="btn btn-default">Reset</button>
<button type="button" onclick="add_row()" id="btnSave" style="background-color:#003300" class="btn btn-default">Save</button>
</div>
</div>
</div>
</div>
<!--End of Add---------------------------------------------------------------------------------------------->
我想将这些输入保存到数组中,如
var ArrCode = [];
所以当用户按下保存按钮时,数组将被填充,如
ArrCode = [input1, input2, input3, etc];
*我不确定是否会创建另一个变量,所以我只会创建它。
如果您需要更多代码段,请与我们联系。
答案 0 :(得分:0)
试试这个,如果有了你的观点
codeArr[table_len] = code;
nameArr[table_len] = name;
不确定,如果你的需要是对的