【发布时间】:2020-07-14 07:16:00
【问题描述】:
我需要在另外 2 个输入字段中复制 id 为“test-id”的输入值。当我使用这个时:
// start copy implementation
function copyTextValue() {
if(document.getElementById('accetta-condizioni').checked){
let text1 = document.getElementById('billing_phone').value;
document.getElementById('reg_username').value = text1;
document.getElementById('reg_password').value = text1;
}
else{
document.getElementById('reg_username').value = "";
document.getElementById('reg_password').value = "";
}
}
在这个html中
<input type="tel" class="input-text" name="billing_phone" id="test-id" value="" placeholder="Number...">
<input type="checkbox" id="acpt-id" name="accetta-condizioni" onclick="copyTextValue();">
<input type="text" id="reg_username" autocomplete="username" value="">
<input type="password" id="reg_password" autocomplete="new-password">
导致字段值“未定义”。为什么?
【问题讨论】:
-
在 jsfiddle 中测试,您的代码似乎运行良好,您能否更清楚地提供您的案例? :jsfiddle.net/43pf6xhu
-
davidepica.it/progetto/birillo/?page_id=3818 在这里试试...我不知道为什么不工作,字段 1 是“billing_phone”
-
document.getElementById('test-id').value;即将无法读取 null 的属性“值”,表示此处 id 错误或该字段不存在。因为我试图改变属性哟东西 document.getElementById('reg_username').value = "blah blah";它的工作原理是“text-id” id 或该字段的问题
-
document.getElementById('billing_phone') 是网站中的脚本..但不工作
-
@davidebr90 我已经浏览了您的来源。您是否注意到字段 1 在
标记中?
和 都有相同的 id="billing_phone",所以当你使用 document.getElementById('billing_phone') 时,你会得到
标签,而不是输入标签,当然
标签有没有属性“值”,你会得到“未定义”。如果尝试获取输入的值,请尝试设置其他id作为输入并再次查询。
标签: javascript input copy field getelementbyid