在包含带有jquery或js的html的变量中禁用change属性

change property disabled in a variable containing html with jquery or js

本文关键字:change 属性 变量 html jquery js 包含带      更新时间:2023-09-26

我需要禁用类"gcf_crud"中变量中的元素。

我的错误代码是:

var defText = ''+
'   <div class="form-group col-md-12">'+
'       <h4 id="minimum-setp">{{title}}</h4>'+
'       <input type="text" class="form-control gcf_crud" id="txtUsuari" value="{{data}}"/>'+
'   </div>';
var defTextDisabled = $(defText).find('.gcf_crud').prop('disabled', true);

有了这段代码,我只获得了输入,但我需要所有原始的html。

我该怎么做才对?

谨致问候,Seak

由于.find('.gcf_crud'),您的defTextDisabled变量仅返回输入。

但是,您似乎需要一个对包含所有元素的jQuery对象的引用(变量)。为了做到这一点,请按步骤分解您的流程:

var defText = '<div class="form-group col-md-12">'
            +   '<h4 id="minimum-setp">{{title}}</h4>'
            +   '<input type="text" class="form-control gcf_crud" id="txtUsuari" value="{{data}}"/>'
            + '</div>',
    $defText = $(defText); // Save the entire thing here
    
// Now, you can disable the input
$defText.find('.gcf_crud').prop('disabled', true);
// And use the whole content
$('body').append($defText);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>

gcf_crud类添加到主分区。