从asp代码后面调用jquery点击事件

本文关键字:jquery 事件 调用 asp 代码 | 更新日期: 2023-09-27 18:24:01

这段代码的前端已经开发完成,并在多个地方使用jquery来拦截用户的点击。这里我有一个单选按钮,当点击时,会触发下面的事件。我需要在页面加载事件上的代码隐藏中为html输入元素启用这一功能,以预填充单选按钮。我该怎么做?

要激发的Jquery事件:

    //checkboxes
    $('.questions-form a.toggle').bind('click', function (event) {
           //jquery magic
     }

html元素,我需要选择或单击:

                            <div id="form-error" style="display: none"></div>
                            <div class="questions-form" id="shipping-questions-form">
                                <div class="question question-textarea has-subquestions">
                                    <div class="question-intro clearfix">
                                        <h2>Did You Receive The Product As Ordered?</h2>
                                        <div class="no-yes answer-acceptable">
                                            <div class="no"><label class="label-1" for="shipping_question_ID_product_received_as_ordered_not_acceptable">Not Acceptable</label></div>
                                            <a href="#" class="toggle"></a>
                                            <div class="yes"><label class="label-2" for="shipping_question_ID_product_received_as_ordered_acceptable">Acceptable</label></div>
                                            <label class="universal-label"></label>
                                            <input type="radio" id="shipping_question_ID_product_received_as_ordered_not_acceptable" name="shipping-question-ID-product-received-as-ordered" value="Not Acceptable" runat="server">
                                            <input type="radio" id="shipping_question_ID_product_received_as_ordered_acceptable" name="shipping-question-ID-product-received-as-ordered" value="Acceptable" checked="true" runat="server">
                                        </div>
                                    </div>

从asp代码后面调用jquery点击事件

您可以使用

ScriptManager.RegisterStartupScript Method (Page, Type, String, String, Boolean);

其中参数为

**page**
Type: System.Web.UI.Page
The page object that is registering the client script block.
**type**
Type: System.Type
The type of the client script block. This parameter is usually specified by using the typeof operator (C#) or the GetType operator (Visual Basic) to retrieve the type of the control that is registering the script.
**key**
Type: System.String
A unique identifier for the script block.
**script**
Type: System.String
The script to register.
**addScriptTags**
Type: System.Boolean
true to enclose the script block with <script> and </script> tags; otherwise, false.

使用示例参见:如何在asp.net c#中调用jquery函数调用?

以下代码执行了所需的功能。我通过"单击"附加到对象的锚点来调用此函数。

<div class="no"><label class="label-1" for="shipping_question_ID_product_received_as_ordered_not_acceptable">Not Acceptable</label></div>
<a href="#" class="toggle" runat="server" id="shipping_question_ID_product_received_as_ordered_link"></a>

然后在后面的代码中:

var autoRunScript = string.Format("<script>$(function() {{ $('#{0}').click(); }} );</script>", shipping_question_ID_product_received_as_ordered_link.ClientID);
Page.RegisterClientScriptBlock("keyClientBlock", autoRunScript);