简体   繁体   English

jQuery函数未在asp.net用户控件中触发

[英]Jquery function is not getting fired in asp.net user control

I have created a user control which contains RadioButtonList. 我创建了一个包含RadioButtonList的用户控件。 Based on the RadioButtonList selection a particular would be visible or hidden. 根据RadioButtonList的选择,特定的对象将是可见的或隐藏的。 Following is my HTML code: 以下是我的HTML代码:

<tr>
                            <td align="left" valign="middle">
                            <font size="2" face="Verdana, Arial, Helvetica, sans-serif">
                            Are you the Financial Analyst/Manager responsible for this profit center?
                            </font>
                            </td>
                            <td align="left" valign="middle">
                            <asp:RadioButtonList ID="RadioButtonListYesNo" runat="server" RepeatDirection="Horizontal">
                            <asp:ListItem Value="1">Yes</asp:ListItem>
                            <asp:ListItem Value="2">No</asp:ListItem>
                            </asp:RadioButtonList>
                            </td>
                            </tr>

<tr>
                        <td>
                        <div id="divFAFMQues" style="visibility:hidden;">                                               
                        <font size="2" face="Verdana, Arial, Helvetica, sans-serif">
                        Who is the FM/FA of this profit center?
                        </font> 
                        </div>                      
                        </td>
                        <td>
                        <div id="divFAFM" style="visibility:hidden;">
                        <input name="FAFM" type="text" id="TextFAFM" maxlength="20"/> 
                         </div>
                        </td>
                        </tr>

Following is my Jquery function: 以下是我的Jquery函数:

 <script src="~/Scripts/jquery-1.11.2.js" type="text/javascript">
        $(document).ready(function () {

            $("input[id$=RadioButtonListYesNo]").change(function () {
                alert("In Jquery");
                var res = $('input[type="radio"]:checked').val();

                if (res == '1') {
                    $("#divFAFMQues").css("visibility", "hidden");
                    $("#divFAFM").css("visibility", "hidden");
                }
                else {

                    $("#divFAFMQues").css("visibility", "visible");
                    $("#divFAFM").css("visibility", "visible");
                }
            });
        });
    </script>

The Jquery function is not getting fired at all, I could not make the alert to get executed. Jquery函数根本没有被触发,我无法发出执行警报。 Where am I going wrong here? 我在哪里错了? Do I need to add any more jquery files to my solution? 我是否需要在解决方案中添加更多jquery文件?

you need to separate the inclusion of jquery and your js code, something like this 您需要将包含的jQuery和您的js代码分开,像这样

<script src="~/Scripts/jquery-1.11.2.js" type="text/javascript"></script>
<script type="text/javascript">
  $(document).ready(function () {

        $("input[id^=RadioButtonListYesNo]").change(function () {
            alert("In Jquery");
            var res = $('input[type="radio"]:checked').val();

            if (res == '1') {
                $("#divFAFMQues").css("visibility", "hidden");
                $("#divFAFM").css("visibility", "hidden");
            }
            else {

                $("#divFAFMQues").css("visibility", "visible");
                $("#divFAFM").css("visibility", "visible");
            }
        });
    });

</script>

use 2 separate script tag, one for adding a js script file and another for inline js code. 使用2个单独的script标签,一个用于添加js脚本文件,另一个用于内联js代码。

Now, jquery says: 现在,jQuery说:

$('[id^=hello]') selects all elements that have an ID beginning with hello. $('[id^=hello]')选择所有ID以hello开头的元素。
$('[id$=hello]') selects all elements that have an ID ending with hello. $('[id$=hello]')选择ID以hello结尾的所有元素。

with your code asp.net generates 2 input of type radio with id RadioButtonListYesNo_0 and RadioButtonListYesNo_1 . 使用您的代码asp.net生成2个类型为radio的输入,其ID为RadioButtonListYesNo_0RadioButtonListYesNo_1 In your code your are using the selector id$=RadioButtonListYesNo that will never match any of the inputs, so, you need to change it for id^=RadioButtonListYesNo as i wrote above. 在您的代码中,您使用的是永远不会匹配任何输入的选择器id$=RadioButtonListYesNo ,因此,您需要将其更改为id^=RadioButtonListYesNo ,如上所述。

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

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