2015-02-07 50 views
1

我有一个文本框和一个按钮,我想从文本框发送到一个函数,当按钮被点击时,似乎无法弄清楚。淘汰赛发送从输入值到功能

 Reg:<input type="text" placeholder="Enter Reg" data-bind="text: search" /> 
<button class="btn btn-danger" data-bind="click: function() { GJobs(search) }" >Get Jobs</button> 

功能视图模型

self.GJobs = function (search) { 
     GetJobs(search) 
    } 

Ajax调用

function GetJobs(search) { 
     $.ajax({ 
      type: "GET", 
      url: 'api/mechanicphone', 
      data: { reg: search}, 
      dataType: 'json', 
      contentType: 'application/json', 
      success: function (data) { 
       self.Jobs(data); 
      }, 
      error: function (data) { 
       $('#MechMobile').html('<h3>Error in retrieval</h3>'); 
      } 
     }); 
    } 

通过REG快到空,当我调试的get

回答

2

有一些事情,应该在您的代码中进行更改。

首先,为了从输入字段中value结合返回值应该是用来代替`文本:

<input type="text" placeholder="Enter Reg" data-bind="value: search" /> 

然后您单击处理最好应调用函数在您的视图模型,然后可以得到可观察变量的值为search。总之,您可以更新您的HTML代码:

Reg:<input type="text" placeholder="Enter Reg" data-bind="value: search" /> 
<button class="btn btn-danger" data-bind="click: GJobs" >Get Jobs</button> 

而且你的视图模型是这样的:

self.GJobs = function() { 
    GetJobs(self.search()); 
} 
self.search = ko.observable(''); 
function GetJobs(searchValue){ 
    // Call web service 
} 
+0

愚蠢的错误。干杯 – user2790781 2015-02-07 11:24:55