繁体   English   中英

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

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

我想在按钮的onclick事件上传递一个值,以便javascript可以更新隐藏字段的值。 但是我无法将字符串传递给js函数

JS

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

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">

您可以使用onclick将值发送给函数,只需像通常调用函数一样将其传递即可。

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

您可以使用bind Bind允许您设置一些默认参数和this上下文。 因此,您可以执行以下操作: sethdnfiletype.bind(this, 1); 它将值1作为第一个参数传递给函数,并将事件作为第二个参数传递给函数。

暂无
暂无

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

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