使用jquery打印出数据属性值

时间:2014-06-12 20:27:12

标签: javascript jquery html

我得到“未定义”打印3次(正确的次数)。如何获取找到的输入的数据绑定值?

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>

1 个答案:

答案 0 :(得分:2)

您不应该包含data-。它应该是:

self.data("bind")