简体   繁体   中英

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.

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. I want the value to be Yes or No but it is not getting inside the if statement. 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

http://jsfiddle.net/o8bm0vxx/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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