简体   繁体   English

如何使用敲除数据绑定在新选项卡中打开按钮链接

[英]How to open button link in new tab using knockout data-bind

I am trying to use a button to open a link into a new tab. 我正在尝试使用按钮打开到新标签页的链接。 The issue is that I am using a data-bind attr: href and using target="_blank" does not work because the target function requires the href not to be in a data-bind. 问题是我正在使用数据绑定attr:href,而无法使用target =“ _ blank”,因为目标函数要求href不在数据绑定中。

I have gotten it to work when not using a data-bind, but a data-bind is necessary for sending information between the two pages. 当不使用数据绑定时,我已经开始使用它了,但是数据绑定对于在两个页面之间发送信息是必需的。

<a data-bind="attr: {href: '@Url.Action("Data", "Report")/?serialNumber='+SN()}" class="btn btn-warning createNew"> View Report </a>

This code is the working code but opens in the same tab. 该代码是有效代码,但在同一选项卡中打开。

When I add target="_blank" outside of the data-bind, I get an error saying that the target function requires href to be used. 当我在数据绑定之外添加target =“ _ blank”时,我收到一条错误消息,指出目标函数需要使用href。

When I add target: "_blank" into the data-bind after the href, it does not do anything and the button no longer works. 当我在href后面的数据绑定中添加目标:“ _ blank”时,它什么也没有做,并且按钮不再起作用。

Currently, the button works and brings in the information I need, I just want it to open into a new tab. 目前,该按钮可以正常工作,并可以提供我需要的信息,我只希望它打开一个新选项卡。

You can simply do this: 您可以简单地做到这一点:

<a data-bind="attr: {href: '@Url.Action("Data", "Report")/?serialNumber='+SN()}" class="btn btn-warning createNew" target="_blank"> View Report </a>

You can ignore the HTML validation error since the validator does not - and cannot - know about the dynamically added href attribute. 您可以忽略HTML验证错误,因为验证器不会(也不能)知道动态添加的href属性。 It's not a problem. 这不是一个问题。

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

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