简体   繁体   English

如何在点击事件之前通过javascript修改按钮的值?

[英]how to modify value of button via javascript before call click event?

I need to modify value of my button via javascript and then, call click method. 我需要通过javascript修改按钮的值,然后调用click方法。

 <script type="text/javascript">
        function callPagerButton(x)
        {                
            $("#btnPager").attr('value', x);
            $("#btnPager").prop('value', x);
            $("#btnPager").html(x);
            $("#btnPager").click();        
        }
    </script>

The problem here is that in sender parameter of click event, i cant see the changes made to the button via javascript: pageBtn.Attributes("value") = "Nothing" . 这里的问题是在click事件的sender参数中,我看不到通过javascript对按钮所做的更改: pageBtn.Attributes("value") = "Nothing" Any ideas? 有任何想法吗? I'm using ASPX 我正在使用ASPX

Protected Sub turnPage(ByVal sender As Object, ByVal e As EventArgs)

    Dim pageBtn = CType(sender, Button)

End Sub

Anything changed on the client-side won't be available on the server-side. 客户端上的任何更改都不会在服务器端上提供。 Buttons only postback the value attribute IF the button triggered a postback to the server; 如果按钮触发了回发到服务器,则按钮仅回发value属性。 with web forms, some button click actions are javascript-generated so that might cause a different behavior. 使用网络表单,某些按钮单击操作是由javascript生成的,因此可能会导致不同的行为。

If you need to submit some piece of data to the server, I'd recommend add: 如果您需要向服务器提交一些数据,我建议添加:

<asp:HiddenField id="hdn" runat="server" ClientIDMode="Static" />

Anything given to the hidden field's value attribute will be available on the server: 任何提供给隐藏字段的value属性的内容将在服务器上可用:

$("#hdn").val("TEST");

On server: 在服务器上:

var txt = hdn.Value; //TEST

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

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