禁用输入字段,因此“getElementById(field_name).value ="将不再改变它的价值 [英] Disable a input field so "getElementById(field_name).value =" will no longer change its value

查看:24
本文介绍了禁用输入字段,因此“getElementById(field_name).value ="将不再改变它的价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个输入字段,它根据其内容被禁用或未禁用.但是我也有一个按钮,它使用 javascript 更改此字段的值getElementById('field_name').value = "something".

是否有可能让它在禁用时不会改变字段的值?
我已经尝试将字段设置为只读和禁用,但这并不能阻止按钮更改其值

I have an input field which is disabled or not based on its contents. However I also have a button which changes the value of this field using the javascript getElementById('field_name').value = "something".

Is it possible to have it so that this will not change the value of the field if it is disabled?
I have tried both setting the field to readonly and disabled, but this doesn't stop the button from changing its value

推荐答案

最简单的方法可能是在按钮的 click 处理程序中执行检查.

The simplest way is probably to perform the check in your button's click handler.

而不是这样做:

document.getElementById("field_name").value = "something";

做:

var element = document.getElementById("field_name");
if (!element.disabled) {
    element.value = "something";
}

这篇关于禁用输入字段,因此“getElementById(field_name).value ="将不再改变它的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆