Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 一旦输入被输入,将按钮禁用设置为false?_Javascript_Html - Fatal编程技术网

Javascript 一旦输入被输入,将按钮禁用设置为false?

Javascript 一旦输入被输入,将按钮禁用设置为false?,javascript,html,Javascript,Html,我有一个小问题,我的按钮不能正常工作,我想。所以,我试图让我的按钮设置为禁用为假,一旦文本输入已输入到输入文本字段。但目前它只在文本字段焦点改变后才起作用 我已经添加了我的代码作为示例,但我真的不确定如何在不需要单击文本字段的情况下立即执行此操作 函数checkInput(){ var tbValue=document.getElementById('tb').value; 如果(tbValue!=''){ document.getElementById('next')。disabled=fa

我有一个小问题,我的按钮不能正常工作,我想。所以,我试图让我的按钮设置为禁用为假,一旦文本输入已输入到输入文本字段。但目前它只在文本字段焦点改变后才起作用

我已经添加了我的代码作为示例,但我真的不确定如何在不需要单击文本字段的情况下立即执行此操作

函数checkInput(){
var tbValue=document.getElementById('tb').value;
如果(tbValue!=''){
document.getElementById('next')。disabled=false;
}
}
.btn{
宽度:75px;
高度:30px;
}
.输入元素{
高度:24px;
}

您可以使用
keyup
(释放任何键后触发)事件或
oninput
(值已更改)代替
onchange
(仅当输入失去焦点时触发)

const nextButton=document.getElementById('next');
常量输入=document.getElementById('tb');
函数checkInput(){
nextButton.disabled=!input.value;
}
.btn{
宽度:75px;
高度:30px;
}
.输入元素{
高度:24px;
}

我建议如下:

const tb = document.getElementById('tb');

// use JavaScript to bind your event-handling, rather than
// using inline event-handlers in the HTML; this makes
// future maintenance a little easier since you only have
// one file to check.
// Here, we're reacting the 'change' event (which includes
// keypress, keyup, paste and most other events in which
// the value of the element is likely to change):
tb.addEventListener('input', checkInput);

// we've condensed your function a little:
function checkInput() {

  // retrieve the element you want to change and assign
  // it to a named variable:
  const button = document.getElementById('next');

  // here we update the disabled property; if the trimmed
  // length of the input's value (the 'this') is zero,
  // the button is true; otherwise it is false:
  button.disabled = this.value.trim().length === 0;
}
const tb=document.getElementById('tb');
tb.addEventListener('input',checkInput);
函数checkInput(){
const button=document.getElementById('next');
button.disabled=this.value.trim().length==0;
}
.btn{
宽度:75px;
高度:30px;
}
.输入元素{
高度:24px;
}

如果您接受jquery,那么这就是您的解决方案

$(“#tb”)。关于(“输入”,函数()
{
设val=$(this.val();
如果(值长度>0)
{
$(“#下一步”).attr(“禁用”、“禁用”);
}
其他的
{
$(“#下一步”).removeAttr(“禁用”);
}
})

尝试使用keyup
函数checkInput(){
var tbValue=document.getElementById('tb').value;
如果(tbValue){
document.getElementById('next')。disabled=false;
}否则{
document.getElementById('next')。disabled=true;
}
}
.btn{
宽度:75px;
高度:30px;
}
.输入元素{
高度:24px;
}

应使用“oninput”事件,而不是“onchange”事件。