admin管理员组文章数量:1414893
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 badges2 Answers
Reset to default 2First 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.
本文标签: javascriptHow to pass value from cshtml file to the JSStack Overflow
版权声明:本文标题:javascript - How to pass value from cshtml file to the JS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745160419a2645414.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论