我有一个验证功能和一个重置功能,确认是否确认并且它们都不起作用。请帮忙。我已经尝试了更多引用html表单输入的方法,即getElementById()函数,getElementByName(),document.form.inputElementName.value,document.forms [“formName”] [“inpuElementName”]。value。没有工作!!!!请帮忙!!!!它是一个类项目
Javascript:
<script type=text/javascript>
function validateInventory() {
var imei_no = document.form.imei_no.value;
var phone_name = document.form.phone_name.value;
var phone_storage = document.form.phone_storage.value;
var phone_color = document.form.phone_color.value;
var phone_buyingprice = document.form.phone_buyingprice.value;
var valid = true;
if (imei_no.value.length > 15 || imei_no.value.length <= 0||imei_no) {
alert("Invalid No. of digits in IMEI");
form.imei_no.focus();
valid = false;
} else {
if (isNaN(imei_no) == true
|| isNaN(phone_buyingprice) == true) {
alert("Please enter number value in IMEI/Buying Price!");
imei_no.focus();
phone_buyingprice.focus();
valid = false;
} else if (imei_no = null ||imei_no = 0 || phone_name_value == ""
|| phone_storage_value == "" || phone_color_value == ""
|| phone_buyingprice_value == 0|| phone_buyingprice_value == null) {
alert("Some entries are empty. Please recheck your Input");
valid = false;
}
}
var cofirmInput= window.confirm("Is your Input correct?");
if(confirmInput==true){
alert("Input succesful!!!");
return valid;
}else{
valid=false;
}
};
function resetInventory() {
var resetForm = window
.confirm("Are you sure you want to reset the form?");
if (resetForm == true) {
alert("Reset Successful!!!");
return true;
}else{
return false;
};
};
这是我的JSP中的HTML表单:
<form action=# method=post name=InventoryForm id=InventoryForm
onreset="return resetInventory()"
onsubmit="return validateInventory()">
<table border="1" cellpadding="5" id=InventoryFormTable>
<tr>
<th>IMEI NO</th>
<th>PHONE NAME</th>
<th>PHONE STORAGE</th>
<th>PHONE COLOR</th>
<th>PHONE BUYING PRICE</th>
</tr>
<tr>
<td><input type=text name=imei_no id=imei_no></td>
<td><select name=phone_name id=phone_name>
<option value=""></option>
<option value="iPhone X">iPhone X</option>
<option value="iPhone 8">iPhone 8</option>
<option value="iPhone 8 Plus">iPhone 8Plus</option>
<option value="iPhone 7">iPhone 7</option>
<option value="iPhone 7 Plus">iPhone 7Plus</option>
<option value="iPhone SE">iPhone SE</option>
<option value="iPhone 6">iPhone 6</option>
<option value="iPhone 6 Plus">iPhone 6Plus</option>
<option value="iPhone 6s">iPhone 6s</option>
<option value="iPhone 6s Plus">iPhone 6sPlus</option>
<option value="iPhone 5">iPhone 5</option>
<option value="iPhone 5s">iPhone 5s</option>
<option value="iPhone 5c">iPhone 5c</option>
<option value="iPhone 4">iPhone 4</option>
<option value="iPhone 4s">iPhone 4s</option>
<option value="iPhone 3G">iPhone 3G</option>
<option value="iPhone 3GS">iPhone 3GS</option>
</select></td>
<td><select name=phone_storage id=phone_storage>
<option value=""></option>
<option value=16GB>16 GB</option>
<option value=3216GB>32 GB</option>
<option value=64GB>64 GB</option>
<option value=128GB>128 GB</option>
<option value=256GB>256 GB</option>
</select></td>
<td><input type=text name=phone_color id=phone_color></td>
<td><input type=text name=phone_buyingingprice
id=phone_buyingingprice></td>
</tr>
<tr>
<td colspan=6><div class=button>
<input type=submit id=submit value=submit>
<input type=reset
id=reset value=reset>
</div></td>
</tr>
</table>
</form>
答案 0 :(得分:0)
这就是你通常获得价值的方式。
HTML:
<input type="text" name="something" id="somethingID" value="value" />
JS:
var somethingValue = document.getElementById("somethingID").value;
但是,如果您特别想要提交表单时的值,可以通过添加一些简单的JQuery轻松获取它,这是我之前在我的项目中使用的
HTML:
<form id="firstForm" action="actionpage.php" method="POST" >
<textarea name="userInput" id="userTextArea"></textarea>
<select id="userOptions">//dynamically filled</select>
<input type="submit" value="Input user" />
</form>
JQuery的:
function testFunction() {
$('#firstForm').submit(function() {
var user = $('#userInput').val();
alert(user);
});
}