gpt4 book ai didi

javascript - 在输入类型复选框上使用 JQuery ReplaceWith

转载 作者:行者123 更新时间:2023-12-02 16:35:52 26 4
gpt4 key购买 nike

我正在尝试将带有输入的表转换为值表。我创建了这个 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

http://jsfiddle.net/o8bm0vxx/

关于javascript - 在输入类型复选框上使用 JQuery ReplaceWith,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27933628/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com