我如何切换已禁用的输入雕像 [英] How i can toggle the input statue disabled

查看:81
本文介绍了我如何切换已禁用的输入雕像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我问我在Stackoverflow上搜索的有关此问题的信息之前,发现我可以通过prop或attr禁用或启用输入, 但是,当我尝试实现此功能时(例如以下示例),它不想切换,是否缺少某些内容? 谢谢

before i ask the question i searched on Stackoverflow about this and found out that i can disable or enable the input by prop or attr, but when i tried to implement this - like the following example- it does not want to toggle, am i missing something? thank you

$(function() {

  $('#submit').on('click', function() {
    $('body').prepend('<div class= "item" ></div>');
    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
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='search' id='search' disabled>
<button id='submit'>Submit</button>
<button id='reset'>Reset</button>

推荐答案

您无需检查生成的div的长度,只需按一下禁用的true或false进行播放即可.

You dont need to check the lenght of generated divs, just play with disabled true or false depending on the click:

$(function() {

  $("input").prop('disabled', true);
  $('#submit').on('click', function() {
        $('body').prepend('<div class= "item" ></div>');
        $("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
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='search' id='search'>
<button id='submit'>Submit</button>
<button id='reset'>Reset</button>

这篇关于我如何切换已禁用的输入雕像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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