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. I have the table below.
我有一个表以数组的形式获取用户输入。我试图检查值是否存在。我遇到的问题是输入的名称是name =“detail []”当我尝试使用document.add_quote_form.detail.value ==''我得到的细节是未定义的。我有下表。
<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>
2 个解决方案
#1
0
Not certain what purpose of []
is at id
of element? You can use document.querySelector()
, attribute begins with selector
不确定[]的目的是什么?您可以使用document.querySelector(),属性以selector开头
document.querySelector("[id^=detail]").value = "";
#2
0
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%,你还应该进行服务器端验证
#1
0
Not certain what purpose of []
is at id
of element? You can use document.querySelector()
, attribute begins with selector
不确定[]的目的是什么?您可以使用document.querySelector(),属性以selector开头
document.querySelector("[id^=detail]").value = "";
#2
0
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%,你还应该进行服务器端验证