简体   繁体   English

如何在html.actionlink中仅调用javascript函数

[英]How to call ONLY javascript function in html.actionlink

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. 为了扩展一个问题,我在同一视图上有一个表格和一个带有4个文本框的表格。 I need to fill one textbox with a value of a specific column in table when user click on link in that column. 当用户单击该列中的链接时,我需要用表中特定列的值填充一个文本框。

try this : 尝试这个 :

@Html.ActionLink("LinkText", "#", "", null, new { onclick="SetTextBoxValue('mydata')" });

Hope this helps. 希望这可以帮助。

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>

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

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