简体   繁体   English

在按钮的onclick事件中将字符串传递给JavaScript函数

[英]Pass string to JavaScript Funtion on onclick event of a button

I want to pass a value on onclick event of a button so that javascript may update the value of hidden field. 我想在按钮的onclick事件上传递一个值,以便javascript可以更新隐藏字段的值。 But I am unable to pass string to js function 但是我无法将字符串传递给js函数

JS JS

<script>
   function sethdnfiletype(filetype)
   {
       $("#hdnfiletype").val(filetype);
   }
</script>

HTML HTML

<asp:ImageButton ID="btn1" runat="server" ToolTip="Upload Report"  OnClick="sethdnfiletype(// here I want to send a number like 1 or 2 or 3 //);" href="#uploadTOEFilePopup" data-toggle="modal" ClientIDMode="Static">

You can send values to a function using onclick, just pass them in as you would call a function usually. 您可以使用onclick将值发送给函数,只需像通常调用函数一样将其传递即可。

 function clickHandler(amount) { alert('Clicked with ' + amount); } 
 <button type="button" onclick="clickHandler(123);">Click me</button> 

You can use bind . 您可以使用bind Bind allows you to set some default parameters and the this context. Bind允许您设置一些默认参数和this上下文。 So you could do this: sethdnfiletype.bind(this, 1); 因此,您可以执行以下操作: sethdnfiletype.bind(this, 1); which would pass in the value 1 to your function as the first parameter and the event as the second parameter. 它将值1作为第一个参数传递给函数,并将事件作为第二个参数传递给函数。

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

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