【问题标题】:jquery set input-field to readablejquery将输入字段设置为可读
【发布时间】:2013-01-28 14:14:40
【问题描述】:

我会在可读和只读之间切换输入字段。我可以将字段设置为只读而没有问题,但其他方式不起作用。你知道为什么吗?

$('.infinite_checkbox').live('click', function(e) {      
if ($('#amount_input_'+id).attr("disabled") != true)
  { 
    $('#amount_input_'+id).val('');
    $('#amount_input_'+id).attr("disabled", true); 
  } else {
    $('#amount_input_'+id).attr("disabled", false);
  }
});

我也尝试过 .removeAttr("disabled")

感谢您的帮助!

【问题讨论】:

  • live 在 jQuery 1.9 上被删除了当你升级 jQuery 时你的代码将无法工作
  • 检查this fiddle

标签: jquery html input


【解决方案1】:

好吧,您使用的不是readonly,而是disabled,但没关系。使用.prop()

$("#amount_input_" + id).prop('disabled', true);
$("#amount_input_" + id).prop('disabled', false);

不相关,但如果您使用的是 jQuery 1.7+,请将 .live 更改为使用 .on

【讨论】:

    【解决方案2】:

    您可以测试此代码

    $('.infinite_checkbox').bind("click",function(In){
       if($(In.target).attr("checked")=="checked"){
          $('#amount_input_'+id).prop("disabled",true);
       }else{
          $('#amount_input_'+id).prop("disabled",false);
       }
    });
    

    【讨论】:

      【解决方案3】:

      $('#amount_input_'+id).attr("readonly","");

      【讨论】:

      • 稍微解释一下就好了。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-25
      • 1970-01-01
      • 2019-05-07
      • 2013-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多