我正在尝试将带有输入的表转换为值表。我创建了这个 JavaScript 函数,它适用于除带有类型复选框的输入之外的所有内容。
这是我当前的功能:
function PopulateHidden() {
$('#Units input').replaceWith(function () {
return this.value;
});
$('#Units select').replaceWith(function () {
return this.value;
});
var html = $("#Units").html();
$('#<%=UnitTableHtml.ClientID%>').val(html);
}
我尝试过此方法的各种版本:
$(".chk").replaceWith(function () {
alert(this.html());
if (this.prop('checked') == true) {
alert("YES");
return "Yes";
};
return "No";
});
但我一直将文本框的值设置为“on”而不是 true/false。我希望该值是"is"或“否”,但它没有进入 if 语句。为什么我无法将复选框的值作为 bool 值获取,有什么原因吗?
这是我定义复选框的方式:
<input type="checkbox" class="chk" style="width:20px;height:20px;"/>
最佳答案
所有人都称赞贾斯珀,因为我正在使用相同的建议构建这个 fiddle :需要 $(this)
而不是 this
关于javascript - 在输入类型复选框上使用 JQuery ReplaceWith,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27933628/