如何将Knockout ahref值传递给jquery变量

时间:2017-03-16 12:58:58

标签: php jquery knockout.js

  

Knockout js Data

<a data-bind="attr: { href: URLPath }" type="submit">Download</a>
  

我将“A tag”的值视为www.ex.com/sp/file.zip

脚本

function DataItem(data) {
    this.URLPath = ko.observable(data.path); //www.ex.com/sp/file.zip
}
  

jQuery脚本

$("input[type=submit]").click(function () {
    $.post('fs.php', 'sp=' + $(this).val(), function (data) {
      -----------
      ----------- 
    });
});

我需要data-bind="attr: { href: URLPath }"(www.ex.com/sp/file.zip)jQuery变量中的值“SP =”(www.ex.com/sp/file.zip)是可能的吗?

1 个答案:

答案 0 :(得分:0)

function DataItem(data) {
 this.URLPath = ko.observable(data.path); 
}

var mymodel = new DataItem(data);

$(document).ready(function() {
  ko.applyBindings(mymodel);
 $("input[type=submit]").click(function (event) {
    $.post('fs.php', 'sp=' + mymodel.URLPath(), function (data) {
    });
 });
});