如何切换输入雕像禁用

在我问问我在Stackoverflow上搜索这个问题并发现我可以通过prop或attr禁用或启用输入,但是当我试图实现这个 – 比如下面的例子 – 它不想切换,我错过了什么? 谢谢

$(function() { $('#submit').on('click', function() { $('body').prepend('
'); if ($('.item').length > 0) { $('#search').removeAttr('disabled'); } if ($('.item').length == 0) { $('#search').attr('disabled', 'disabled'); //$('#search').prop('disabled', true); } }); $('#reset').on('click', function() { $('.item').remove(); }) });
 button { display: inline-block } input { position: fixed; bottom: 0; right: 0 } .item { width: 50px; height: 50px; background: orangered; margin: 5px } 
     

您不需要检查生成的div的长度,只需根据点击播放禁用的true或false:

 $(function() { $("input").prop('disabled', true); $('#submit').on('click', function() { $('body').prepend('
'); $("input").prop('disabled', false); }); $('#reset').on('click', function() { $('.item').remove(); $("input").prop('disabled', true); }) });
 button { display: inline-block } input { position: fixed; bottom: 0; right: 0 } .item { width: 50px; height: 50px; background: orangered; margin: 5px } 
     

对于您的HTML,请使用以下命令:

    

请注意[disabled =“disabled”],然后您的JavaScript使用:

  

这应该工作。