简体   繁体   English

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

[英]Using JQuery ReplaceWith on input type checkbox

I am trying to convert a table with inputs into a table of values. 我试图将带有输入的表转换为值表。 I created this javascript function that works on everything except for input with the type checkbox. 我创建了此javascript函数,该函数对除带有类型复选框的输入之外的所有东西都有效。

This is my current function : 这是我当前的功能:

    function PopulateHidden() {

        $('#Units input').replaceWith(function () {
            return this.value;
        });
        $('#Units select').replaceWith(function () {
            return this.value;
        });

        var html = $("#Units").html();
        $('#<%=UnitTableHtml.ClientID%>').val(html);
    }

i have tried doing various version of this method: 我试图做这种方法的各种版本:

        $(".chk").replaceWith(function () {
            alert(this.html());
            if (this.prop('checked') == true) {
                alert("YES");
                return "Yes";
            };
            return "No";
        });

But I keep getting the value of the textboxes as "on" rather than a true/false. 但是我一直将文本框的值设置为“ on”,而不是true / false。 I want the value to be Yes or No but it is not getting inside the if statement. 我希望该值为Yes或No,但它不在if语句内。 Any reason why I cannot get the value of the checkbox as a boolean? 为什么无法将复选框的值作为布尔值获取任何原因?

Here is how i defined the checkbox: 这是我定义复选框的方式:

<input type="checkbox" class="chk" style="width:20px;height:20px;"/>

All praise Jasper as I was building this fiddle with the same suggestion: Needs $(this) instead of this 在我用相同的建议构建此小提琴时,所有人都赞扬了贾斯珀:需要$(this)而不是this

http://jsfiddle.net/o8bm0vxx/ http://jsfiddle.net/o8bm0vxx/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM