如何在 html.actionlink 中只调用 javascript 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26579985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to call ONLY javascript function in html.actionlink
提问by onedevteam.com
I have java script function
我有java脚本功能
function SetTextBoxValue(data) {
$('#text_box').val($(this).val("myField"))
};
Is it possible, and how, to call only this function, WITHOUT controller and action names from @Html.ActionLink()
in razor view?
是否有可能以及如何@Html.ActionLink()
在剃刀视图中仅调用此函数而无需控制器和操作名称?
To extend a question, i have table and a form with 4 text boxes on same view. I need to fill one textbox with a value of a specific column in table when user click on link in that column.
为了扩展一个问题,我在同一个视图中有一个表格和一个带有 4 个文本框的表单。当用户单击该列中的链接时,我需要用表中特定列的值填充一个文本框。
回答by Syed Muhammad Zeeshan
try this :
试试这个 :
@Html.ActionLink("LinkText", "#", "", null, new { onclick="SetTextBoxValue('mydata')" });
Hope this helps.
希望这可以帮助。
回答by ihebiheb
You need 2 steps :
您需要 2 个步骤:
1 : add onclick="return someJsFct" in your actionLink
1 : 在 actionLink 中添加 onclick="return someJsFct"
@Html.ActionLink("click here", "", "", null, new { onclick="return SetTextBoxValue('data')" });
2 : your javascript function must return false to prevent the actionLink from calling the server
2:你的javascript函数必须返回false以防止actionLink调用服务器
<script language="javascript">
function SetTextBoxValue(data) {
// some code
return false;
}
</script>