简体   繁体   中英

Print out data attribute value with jquery

I get "undefined" print 3 times (the right amount of times). How can I get the data-bind value for the found inputs?

function DataBindForEdit(dataTag, controller, action) {
    // get the inputs that will be sent to the controller
    var dataBoundControls = $('[data-bind="' + dataTag + '"] [data-bind]');

    // loop over each element
    dataBoundControls.each(function () {
        var self = $(this);

        // for now just print out the value of the data-bind attribute
        alert(self.data("data-bind"));
    });
}

@section Scripts{
    <script>
        $(function () {

            $("#btnSave").click(function(){
                DataBindForEdit("Customer", "Index", "CreateCustomers");
            });
        });
    </script>
}

<form data-bind="Customer">
    Name: <input type="text" data-bind="Name" data-bind-type="text" />
    Birthday: <input type="text" data-bind="Birthday" data-bind-type="text" />
    Address: <input type="text" data-bind="Address" data-bind-type="text" />
    <input type="submit" value="Save" id="btnSave" />
</form>

You should not include data- . It should be:

self.data("bind")

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