2014-06-12 58 views
0

我得到「undefined」打印3次(正確的次數)。如何獲得找到的輸入的數據綁定值?用jquery打印出數據屬性值

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> 

回答

2

您不應該包括data-。它應該是:

self.data("bind") 
+0

我太親近:)。 Thx工作。 – user441521

+0

確實非常接近! – PeterKA