Javascript 如何更改<;的输入值;离子输入></离子输入>;元素通过JS
我试图通过JS为我的离子输入元素赋值。这是我的密码: HTML: 我无法更改输入元素的值,我想将position.coords.latitude+'、'+position.coords.longitude作为输入元素的值Javascript 如何更改<;的输入值;离子输入></离子输入>;元素通过JS,javascript,element,ionic2,Javascript,Element,Ionic2,我试图通过JS为我的离子输入元素赋值。这是我的密码: HTML: 我无法更改输入元素的值,我想将position.coords.latitude+'、'+position.coords.longitude作为输入元素的值 希望得到一个快速的答案。谢谢 假设以数组形式正确接收纬度和经度,代码应如下所示: function onSuccess(position) { document.getElementById('geolocation').setAttribute("value"
希望得到一个快速的答案。谢谢 假设以数组形式正确接收纬度和经度,代码应如下所示:
function onSuccess(position) {
document.getElementById('geolocation').setAttribute("value", latitude + ', ' + longitude);
}
在此处测试:
说明:
在这里,我们只是使用元素的Id来调用元素,并设置其属性值,如下所述:试试这个
document.getElementById("geolocation").value=
(position.coords.latitude + ',' + position.coords.longitude);
尝试以下方法:
[(ngModel)]="position.coords.latitude + ';' + position.coords.longitude"
Dom操作将起作用,但这不是正确的方法,请参见下面的“如何使用解决方案”:
HTML
为什么不使用[(ngModel)]并将对象直接绑定到输入?没有像您这样直接接触DOM?我找到了一个解决方案,我使用var element=document.getElementById('geolocation');element.value=position.coords.latitude+','+position.coords.longitude它起作用了:我很高兴你把它修好了!但这和这个答案加上您之前尝试过的内容是一样的,您可能已经更改了代码中可能出现问题的其他内容。
document.getElementById("geolocation").value=
(position.coords.latitude + ',' + position.coords.longitude);
[(ngModel)]="position.coords.latitude + ';' + position.coords.longitude"
<ion-input type="text" placeholder=" Enter a location" style="padding-left: 10px;" id="geolocation" [(ngModel)]="position.coords.latitude + ';' + position.coords.longitude"></ion-input>
...
export class Geolocation {
//{ position : { coords : { latitude : ... , longitude : ... } } }
position : any;
...
}