我需要添加一个隐藏的表单元素(作为大表单的一部分),该元素需要动态创建和添加。到目前为止我所拥有的一个片段是:
var value = '*****';
var inputHidden = $('<input>').attr('type', 'hidden')).addClass("warningOnly").val(value);
// conditionally disable new element here
$(elem).append(inputHidden);
这按预期工作。我还想做的是在将隐藏输入添加到表单之前有条件地禁用它。我尝试过以下变体:
$(inputHidden).prop('disabled', true);
这不会禁用该元素,我需要一种方法来做到这一点。
最佳答案
工作正常,或者作为 fiddle :
$(function(){
var value = '*****';
var elem = $(".foo");
var inputHidden = $('<input>')
.attr('type', 'hidden')
.addClass("warningOnly")
.val(value);
// conditionally disable new element here
elem.append(inputHidden);
inputHidden.prop("disabled", true);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form class="foo">
Your hidden el will go here. Check the console to see that its disabled.
</form>
关于javascript - 使用 jQuery 动态添加表单函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48451944/