[英]Getting input array value from javascript
I have a table that gets the users input in a form of an array. 我有一个表,以表格的形式获取用户输入。 I am trying to check to see if the values exist. 我试图检查值是否存在。 The problem I run into is the name of the input is name="detail[]"
When I try to use document.add_quote_form.detail.value == ''
I get detail is undefined. 我遇到的问题是输入的name="detail[]"
是name="detail[]"
当我尝试使用document.add_quote_form.detail.value == ''
我得到的细节是不确定的。 I have the table below. 我有下表。
<form id="add_quote_form" name="add_quote_form" class="form-horizontal">
<table style="width: 90%" id="myTable" class="centered-table table table-bordered">
<thead>
<tr>
<th>Item</th>
</tr>
</thead>
<tbody>
<tr>
<td style="width: 60%"><input type="text" id="detail[]" name="detail[]"/></td>
</tr>
</tbody>
</table>
</form>
Not certain what purpose of []
is at id
of element? 不确定[]
目的是在元素的id
上吗? You can use document.querySelector()
, attribute begins with selector 您可以使用document.querySelector()
,属性以选择器开头
document.querySelector("[id^=detail]").value = "";
First Use Class Not Id here if you have alot of td tag with same input name 如果您有很多具有相同输入名称的td标签,则在这里首次使用Class Not Id
<form id="add_quote_form" name="add_quote_form" class="form-horizontal">
<table style="width: 90%" id="myTable" class="centered-table table table-bordered">
<thead>
<tr>
<th>Item</th>
</tr>
</thead>
<tbody>
<tr>
<td style="width: 60%"><input type="text" id="detail[]" name="detail[]"/></td>
</tr>
</tbody>
</table>
</form>
<script>
var details = document.getElementsByClassName('detail'),
details_values = [].map.call(details , function( row ) {
// you can do what ever you need here if you need to check each value
return row.value;
});
alert(details_values);
</script>
however if you using javascript to validate input it should only for interface using because it not secure to rely on javascript 100% , you should also make a server side validation 但是,如果您使用javascript来验证输入,则应该仅用于接口使用,因为不安全地依赖javascript 100%,还应该进行服务器端验证
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.