javascript - How to pass value from cshtml file to the JS - Stack Overflow

Hello I need to pass one param to the my JS function after pressing button.Here is my button:<button

Hello I need to pass one param to the my JS function after pressing button.

Here is my button:

<button type="button" class="btn btn-default" id="UserClick" onclick="foo(@HttpContext.Current.User.Identity.Name)">Click here to add me as a user</button>

as You can see I try to send @HttpContext.Current.User.Identity.Name to the function foo.

Here my Foo function:

    function foo(value) {
         alert(value);
    };

I tried sollutions from those topics: One Two

But all I get get is this error:

Uncaught SyntaxError: Unexpected token ILLEGAL 

How to bypass this?

@update

All files included:

<script src="/Scripts/CreateDevice.js"></script>
<script src="/Scripts/jquery-ui.js"></script>
<script src="/Scripts/jquery-1.10.2.js"></script>

full JS file:

$(document).ready(function () {

function foo(value) {
    alert(value);
};
})

I can access to the js file from page-source.

Hello I need to pass one param to the my JS function after pressing button.

Here is my button:

<button type="button" class="btn btn-default" id="UserClick" onclick="foo(@HttpContext.Current.User.Identity.Name)">Click here to add me as a user</button>

as You can see I try to send @HttpContext.Current.User.Identity.Name to the function foo.

Here my Foo function:

    function foo(value) {
         alert(value);
    };

I tried sollutions from those topics: One Two

But all I get get is this error:

Uncaught SyntaxError: Unexpected token ILLEGAL 

How to bypass this?

@update

All files included:

<script src="/Scripts/CreateDevice.js"></script>
<script src="/Scripts/jquery-ui.js"></script>
<script src="/Scripts/jquery-1.10.2.js"></script>

full JS file:

$(document).ready(function () {

function foo(value) {
    alert(value);
};
})

I can access to the js file from page-source.

Share Improve this question edited May 23, 2017 at 12:11 CommunityBot 11 silver badge asked Jan 17, 2014 at 14:31 szpicszpic 4,49815 gold badges56 silver badges90 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

First of all change you sequence of file to

<script src="/Scripts/jquery-1.10.2.js"></script>
<script src="/Scripts/jquery-ui.js"></script>
<script src="/Scripts/CreateDevice.js"></script>

Declare your function outside document-ready handler. Like

$(document).ready(function () {
});

function foo(value) {
    alert(value);
};

Pass your parameter in quotes

onclick="foo('@HttpContext.Current.User.Identity.Name');"

Additionally, You can bind event using .on() and pass parameter using attributes

JS

$(document).ready(function () {
    //Bind Event
    $(document).on('click', '#UserClick', function(){
        alert($(this).data('user-name'))
    });
});

HTML

<button type="button" 
    class="btn btn-default" 
    id="UserClick" 
    data-user-name="@HttpContext.Current.User.Identity.Name"
    >Click here to add me as a user</button>
onclick="foo('@HttpContext.Current.User.Identity.Name');"


$(document).ready(function () {

window.foo = function(value) {
    alert(value);
};
})

this will work...

You should consider removing the 'onclick' attribute in the html though, and binding the function to the button using jquery's .on() function.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745160420a4614364.html

相关推荐

  • javascript - How to pass value from cshtml file to the JS - Stack Overflow

    Hello I need to pass one param to the my JS function after pressing button.Here is my button:<button

    9小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信