Can';似乎无法将Javascript元素.value解析为HTML<;输入>;领域
没有一个常用的技巧能解决这个问题,不知道发生了什么。。。我有3个“如果”条件,返回3个不同的值,如下所示:Can';似乎无法将Javascript元素.value解析为HTML<;输入>;领域,javascript,html,input,Javascript,Html,Input,没有一个常用的技巧能解决这个问题,不知道发生了什么。。。我有3个“如果”条件,返回3个不同的值,如下所示: if (addressObj.types[j] === 'administrative_area_level_1') { if ((document.getElementById('county_from').value = addressObj.short_name) === 'County Carlow') { document.getElementByI
if (addressObj.types[j] === 'administrative_area_level_1') {
if ((document.getElementById('county_from').value = addressObj.short_name) === 'County Carlow') {
document.getElementById('id_location_value').value = 2;
}
if ((document.getElementById('county_from').value = addressObj.short_name) === 'County Cavan') {
document.getElementById('id_location_value').value = 3;
}
if ((document.getElementById('county_from').value = addressObj.short_name) === 'County Clare') {
document.getElementById('id_location_value').value = 4;
}
当我将信息解析到我在test_位置下创建的“test”数据库列时,它可以正常工作
<input type="hidden" id="id_location_value" name="test_location" />
但我无法让它与我需要用于此项目的实际列一起工作
<input type="hidden" id="location-selected" name="location" value="id_location_value" />
但是,如果我输入一个随机值,比如follow,同样也可以
<input type="hidden" id="location-selected" name="location" value="64" />
我在这里遗漏了什么?问题可能与这一行有关 您正试图在
if
子句中赋值
if ((document.getElementById('county_from').value = addressObj.short_name) ===
^^^
'County Carlow')
根据你问题中的标记 第一个输入有一个id“id\u location\u value” 返回您的输入元素,您可以对其设置值 另一方面,其他2个输入
<input type="hidden" id="location-selected" name="location" value="id_location_value" />
<input type="hidden" id="location-selected" name="location" value="64" />
返回null,浏览器会抛出如下错误
Uncaught TypeError: Cannot set property 'value' of null
您可以将输入id设置为“id\u位置\u值”
或者您可以将JS更改为
document.getElementById('location-selected').value = 2;
可能是因为您的第二次输入选择了
location
作为id
而不是id location value
。是的,我也试过了。。。但为什么在使用像“64”这样的随机值时它能工作,而在使用元素值id\u location\u值时却不能工作?这就是让我困惑的地方…if条件运行良好,并且返回各自的值。实际上,我可以使用数据库中的“test”列解析这些值,但使用实际位置列则无法解析这些值。。。不知道我哪里出了问题???
document.getElementById('id_location_value')
Uncaught TypeError: Cannot set property 'value' of null
document.getElementById('location-selected').value = 2;