Javascript function behaviour

I have a problem with javascript. In my code there is a radio button, on click it should call the UcSelect() function. I tried the code below but it didn't work. The function just try to print something in order to help me to understand the behavior of the code.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js">   

    function UcSelect() {
        alert('hello1');
        if ($("#rbtTipo :checked").val() == 'E') {
            alert("aaaaaaaaaaaaa");
        }
        if ($("#rbtTipo :checked").value == 'E') {
            alert("bbbbbbbbbbbbbbbbbb");
        }
        return true;
    }

</script>

And the radio button:

<asp:RadioButtonList ID="rbtTipo" runat="server" Style="margin-top: 4px; margin-bottom: 8px;" onclick="UcSelect();" >
        <asp:ListItem Value="E">&nbsp; Nuovo Coefficiente Energia</asp:ListItem>
        <asp:ListItem Value="G">&nbsp; Nuovo Coefficiente Gas</asp:ListItem>
    </asp:RadioButtonList>

Answers:

Answer

script elements can have a src attribute or content, but not both. If they have both, the content is ignored (the content is considered "script documentation," not code).

As you are using ASP.NET and rbtTipo is a server control you need to use Control.ClientID.

<%= rbtTipo.ClientID %> will Gets the control ID for HTML markup that is generated by ASP.NET.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js">
</script>
<script>
    function UcSelect() {
        var value = $("#<%= rbtTipo.ClientID %> :checked").val();
        if (value == 'E') {
            alert("aaaaaaaaaaaaa");
        }
        if (value == 'G') {
            alert("bbbbbbbbbbbbbbbbbb");
        }
        return true;
    }
</script>
Answer

You need to update your code. You have to close the script tag properly

Add there is a redundant code of checking value E. You should remove that code or update it to check for G. For your reference, I have updated the code below.

 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
 <script type="text/javascript">

    function UcSelect() {
        alert('hello1');
        if ($("#rbtTipo :checked").val() == 'E') {
            alert("aaaaaaaaaaaaa");
        }
        if ($("#rbtTipo :checked").val() == 'G') {
            alert("bbbbbbbbbbbbbbbbbb");
        }
        return true;
    }

</script>

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.