简体   繁体   English

KnockoutJS数据绑定:点击javascript点击

[英]KnockoutJS data-bind: click from javascript click

Let us say we have a table as so 让我们说我们有一张桌子

<table>
    <tbody data-bind="foreach: orderItems">
        <tr class='datarow'>
            <td data-bind="text: whatever"></td>
            <td><a id='anchor' href='#' data-bind='click: $root.save' onclick='alert("a");'>Text</a></td>
        </tr>
    </tbody>
</table>

so when I do 所以,当我这样做

document.getElementsById('anchor').click();

the alert triggers but $root.save doesn't. 警报触发但$ root.save没有。 Any ideas? 有任何想法吗?

Try This

<script type="text/javascript">
    var viewModel = {

        Save: function() {

            alret("Hello");
        }
    };
ko.applyBindings(new MyViewModel());
</script>
<table>
    <tbody data-bind="foreach: orderItems">
        <tr class='datarow'>
            <td data-bind="text: whatever"></td>
            <td><a id='anchor' href='#' data-bind='click: $root.save' onclick='alert("a");'>Text</a></td>
        </tr>
    </tbody>
</table>

I'm seeing 2 problems with the code: 我看到代码有两个问题:

  1. getElementsById - are you sure this method exists? getElementsById - 你确定这个方法存在吗?

Each ID should only exist once on the page. 每个ID应仅在页面上存在一次。 When you want to find an element by it's ID, you can call getElementById (singular), and it will return an element. 当你想通过它的ID找到一个元素时,可以调用getElementById(singular),它将返回一个元素。 Because only one element should have a given ID, there is no getElementsById (plural) method. 因为只有一个元素应该具有给定的ID,所以没有getElementsById(复数)方​​法。

Your foreach binding combined with a hardcoded 'anchor' id will result in multiple elements with an ID of anchor. 您的foreach绑定与硬编码的“锚”ID结合将导致多个元素具有锚点ID。 This is a no-no. 这是禁忌。 Perhaps use a class of 'anchor' instead of an id of 'anchor'. 也许使用一类'锚'而不是'锚'的id。

<a class='anchor' href='#' data-bind='click: $root.save' onclick='alert("a");'>Text</a>

So my guess is that getElementsById is not returning anything to click on. 所以我的猜测是getElementsById没有返回任何东西来点击。

  1. click() 点击()

Even if you were able to get an element back from the getElementsById, there is no click() method on those elements. 即使你能够从getElementsById获取一个元素,这些元素上也没有click()方法。

As an alternate, you can use jQuery to get the elements, and with a jQuery object you can call the click() method. 作为替代,您可以使用jQuery来获取元素,使用jQuery对象可以调用click()方法。 Or you could take an element returned from getElementById, wrap it in jQuery, then call click(). 或者您可以从getElementById返回一个元素,将其包装在jQuery中,然后调用click()。

$('.anchor').click();

Here is a working example: http://jsfiddle.net/tlarson/t4yZL/1/ 这是一个工作示例: http//jsfiddle.net/tlarson/t4yZL/1/

<table>
    <tbody data-bind="foreach: orderItems">
        <tr class='datarow'>
            <td data-bind="text: whatever"></td>
            <td><a id='anchor' href='#' onclick='alert("a"); viewModel.save(ko.dataFor(this));'>Text</a></td>
        </tr>
    </tbody>
</table>
  • Assume that you stored your viewModel elsewhere. 假设您将viewModel存储在其他位置。
  • ko.dataFor(element) - returns the data that was available for binding against the element ko.dataFor(element) - 返回可用于绑定元素的数据

You may have a look at this link 您可以查看此链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM