Javascript - 将 document.getelementbyid().value 与变量一起使用
Javascript - Use document.getelementbyid().value with a variable
我正在尝试使用 document.getElementById(my_field).value 捕获 HTML 表单上文本字段的值,其中传递了变量 my_field动态地执行我的功能,但我碰壁了。
在这种情况下如何使用变量?
函数似乎不解析变量 my_field 的内容,而是将其视为字符串,无论我使用引号、方括号还是大括号。
function myFunction() {
var my_field = arguments[0];
var current_value = document.getElementById(my_field).value;
alert ("Current Value: " + current_value);
}
我这样做是因为我在一个表单上有多个记录,并且每一行都有自己唯一的必填字段 ID。
运行 以上只是什么都不做。我认为永远不会弹出警报是因为 current_value 永远不会设置。
要添加更多详细信息 - 为了这个问题的目的,我试图简化所有内容,因为还有许多其他不必要的并发症只会影响主要问题 - 在我的 HTML 表单上是一个文本字段,它在 onChange
上调用我的函数
onchange="enforce_multiples('quantity[<?php echo $line_id; ?>]',<?php echo $product['minimum'];?>)"
我已检查是否通过将参数 [0] 和 [1] 的值输出到警报来正确捕获它们。一切正常,直到我尝试设置 quantity_entered 值。
<script>
function enforce_multiples() {
var line_id = arguments[0];
var quantity_increments = arguments[1];
var quantity_entered = document.getElementById([line_id]).value;
alert("QE" + quantity_entered);
//var quantity_mod = quantity_entered % quantity_increments;
//var revised_quantity = quantity_entered - quantity_mod;
//alert("RQ: " + revised_quantity);
//document.getElementById([line_id]).value = revised_quantity;
}
</script>
检查控制台,我收到错误:未捕获类型错误:无法读取 geElementById 行
上的 属性 'value' 空值
你应该写 document.getElementById(my_field)
而不是 document.getelementbyid(my_field)
。
好的,我深入了解一下,以防有人感兴趣。
为了在 document.getElementById() 中使用变量,您只需添加不带引号的变量名称。
var my_variable = "field1";
document.getElementById(my_variable);
这在我的表单上不起作用的原因是因为文本字段只有 name 参数而没有 id 参数。
所以我需要改变:
<input type="text" name="field_name" value="1234" />
到
<input type="text" name="field_name" id="field_name" value="1234" />
这样就解决了。否则我只是在控制台中收到一般的 NULL 错误消息。
我正在尝试使用 document.getElementById(my_field).value 捕获 HTML 表单上文本字段的值,其中传递了变量 my_field动态地执行我的功能,但我碰壁了。
在这种情况下如何使用变量?
函数似乎不解析变量 my_field 的内容,而是将其视为字符串,无论我使用引号、方括号还是大括号。
function myFunction() {
var my_field = arguments[0];
var current_value = document.getElementById(my_field).value;
alert ("Current Value: " + current_value);
}
我这样做是因为我在一个表单上有多个记录,并且每一行都有自己唯一的必填字段 ID。
运行 以上只是什么都不做。我认为永远不会弹出警报是因为 current_value 永远不会设置。
要添加更多详细信息 - 为了这个问题的目的,我试图简化所有内容,因为还有许多其他不必要的并发症只会影响主要问题 - 在我的 HTML 表单上是一个文本字段,它在 onChange
上调用我的函数onchange="enforce_multiples('quantity[<?php echo $line_id; ?>]',<?php echo $product['minimum'];?>)"
我已检查是否通过将参数 [0] 和 [1] 的值输出到警报来正确捕获它们。一切正常,直到我尝试设置 quantity_entered 值。
<script>
function enforce_multiples() {
var line_id = arguments[0];
var quantity_increments = arguments[1];
var quantity_entered = document.getElementById([line_id]).value;
alert("QE" + quantity_entered);
//var quantity_mod = quantity_entered % quantity_increments;
//var revised_quantity = quantity_entered - quantity_mod;
//alert("RQ: " + revised_quantity);
//document.getElementById([line_id]).value = revised_quantity;
}
</script>
检查控制台,我收到错误:未捕获类型错误:无法读取 geElementById 行
上的 属性 'value' 空值你应该写 document.getElementById(my_field)
而不是 document.getelementbyid(my_field)
。
好的,我深入了解一下,以防有人感兴趣。
为了在 document.getElementById() 中使用变量,您只需添加不带引号的变量名称。
var my_variable = "field1";
document.getElementById(my_variable);
这在我的表单上不起作用的原因是因为文本字段只有 name 参数而没有 id 参数。
所以我需要改变:
<input type="text" name="field_name" value="1234" />
到
<input type="text" name="field_name" id="field_name" value="1234" />
这样就解决了。否则我只是在控制台中收到一般的 NULL 错误消息。