你可以使用这个:
HTML
<input type="text" id="email" value="email.com" firstvalue="email.com" />
JS
document.getElementById('email').onkeyup = function () {
var firstvalue = this.getAttribute('firstvalue');
if (this.value != firstvalue) {
alert('You will be rediretioned!');
window.location.href = 'http: //www.valueinhere.com';
}
};
在这里演示
我使用了 onkeyup 事件,因为 onchange 仅在模糊时触发,因此如果他们更改邮件但不单击另一个输入/元素,则不会发生任何事情。
请记住,如果网页不是用户友好的,则更改预期的行为。禁用输入字段和旁边的“更改”按钮可能会更好。
在这种情况下,您可以使用:
HTML
<input type="text" id="email" value="email.com" disabled="disabled" />
<button type="button" id="mailchange">Change this</button>
JS
document.getElementById('mailchange').onclick = function () {
alert('You will be rediretioned!');
window.location.href = 'http: //www.valueinhere.com';
};
在这里演示