【问题标题】:Call jquery click event from asp code behind?从后面的asp代码调用jquery点击事件?
【发布时间】:2014-09-23 16:37:14
【问题描述】:

这段代码前端已经开发好了,多处使用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>

【问题讨论】:

  • 虽然完全有可能,但如果您需要这样做,这可能是一个糟糕的设计。也许您应该将需要服务器端交互的部分与不需要的部分分开?

标签: c# javascript jquery html asp.net


【解决方案1】:

你可以使用

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.

使用示例见:how to call jquery function call in asp.net c#?

【讨论】:

  • 在上面的代码中,我有多个具有相同点击事件的输入。如何为特定的无线电输入调用此函数,例如“shipping-question-ID-product-received-as-ordered”?
  • 把它放在受保护的 void RadioButton1_CheckedChanged(object sender, EventArgs e) { }
  • 我有点困惑...我需要发送点击事件“shipping-question-ID-product-received-as-ordered”,我还能使用您描述的方法吗?回答?
【解决方案2】:

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

<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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-27
    • 2012-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-24
    相关资源
    最近更新 更多