使用knockout将文本框中的输入值绑定到服务器。

时间:2015-03-24 18:24:26

标签: javascript jquery knockout.js

使用knockout我想接受提交的值并使用ko.toJson将它们发送到服务器。我是淘汰赛的新手,我确信我有一些句法问题。更简洁的是,我希望我的提交按钮将值保存在文本框中并将其保存到服务器。 这是我的小提琴https://jsfiddle.net/Domt301/30h3oe9t/

<div class='Agent Form'>   
<p>First name: <input data-bind='value: firstName' /></p> 
<p>Last name: <input data-bind='value: lastName' /></p> 
<p>Agent NPN: <input data-bind='value: NPN' /></p>
<p>Lead Location: <input data-bind='value: leadlocation' />(City, State, Zip)</p>
<p>Requested Lead Delivery Time/Date: </p>
    <p><input data-bind='value: leaddeliverytime' /></p>
<p>Leads Requested: <select>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
    <option value="5">5</option>
    <option value="10">10</option>
    </select> </p>
<p>Email Address: <input data-bind='value: EmailAddress' /></p>
</div>
<div>
        <input type="button" id="btnSubmit" 
          value="Submit" data-bind = "click: submit" />
         <input type="button" id="btnReset" 
           value="Reset" data-bind = "click: reset" />
    </div>

脚本

 var ViewModel = function(first, last, NPN, leadlocation, leaddeliverytime, EmailAddress) {
this.firstName = ko.observable(first);
this.lastName = ko.observable(last);
this.NPN = ko.observable(NPN);
this.leadlocation = ko.observable(leadlocation);
this.leaddeliverytime = ko.observable(leaddeliverytime);
this.EmailAddress = ko.observable(EmailAddress);

};
var jsonData = ko.toJson(viewModel);

1 个答案:

答案 0 :(得分:1)

我有updated原始小提琴。

您可以将submitreset按钮绑定到视图模型内的功能。然后在您创建数据对象的数据模型中创建一个submit函数,在此上下文中我将其定义为Agent并将其传递给ajax调用,您将用/echo/json替换observables发布数据的控制器的路径。

我对你的html进行了一些细微的修改,以跟踪<div class='Agent Form'> <p>First name: <input data-bind='value: firstName' /></p> <p>Last name: <input data-bind='value: lastName' /></p> <p>Agent NPN: <input data-bind='value: npn' /></p> <p>Lead Location: <input data-bind='value: leadLocation' />(City, State, Zip)</p> <p>Requested Lead Delivery Time/Date: <input data-bind='value: leadDeliveryTime' /></p> <p>Leads Requested: <select> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="10">10</option> </select> </p> <p>Email Address: <input data-bind='value: emailAddress' /></p> </div> <div> <button data-bind = "click: submit" >Submit</button> <button data-bind = "click: reset" >Reset</button> </div>

debugger;

var viewModel = function() {
    var self = this;
    self.firstName = ko.observable();
    self.lastName = ko.observable();
    self.npn = ko.observable();
    self.leadLocation = ko.observable();
    self.leadDeliveryTime = ko.observable();
    self.leadsRequested = ko.observable();
    self.emailAddress = ko.observable();
    self.fullName = ko.computed(function() {
        return self.firstName() + " " + self.lastName();
    });

    self.submit = function () {     
        var Agent = {};
        Agent.FirstName = self.firstName();
        Agent.LastName = self.lastName();
        Agent.Npn = self.npn();
        Agent.LeadLocation = self.leadLocation();
        Agent.LeadDeliveryTime = self.leadDeliveryTime();
        Agent.LeadsRequested = self.leadsRequested();
        Agent.EmailAddress = self.emailAddress();
        Agent.FullName = self.fullName();

        $.ajax({
            url: '/echo/json/',
            type: 'POST',
            data: Agent,
            success: function (result) {                
            alert("Recorded inserted Sucessfully");
            self.firstName("");
            self.lastName("");
            self.npn("");
            self.leadLocation("");
            self.leadDeliveryTime("");
            self.leadsRequested("");
            self.emailAddress("");
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            debugger;
            alert("some error");
        }
    });
};

     self.reset = function() {
         self.firstName("");
         self.lastName("");
         self.npn("");
         self.leadLocation("");
         self.leadDeliveryTime("");
         self.leadsRequested("");
         self.emailAddress("");
         alert("Reset");
    };
};



ko.applyBindings(new viewModel());

这是淘汰赛代码:

{{1}}