0

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>

2 Answers 2

2

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>
Sign up to request clarification or add additional context in comments.

9 Comments

Probably needs to use ClientID somewhere as well. The ID on ASP.Net controls is the server-side ID.
Thanks it works, but the statement : $("#rbtTipo :checked") return []
@user3066925, try $("#<%= rbtTipo.ClientID %> :checked").val()
@Satpal, if i put the script in the same UC where the button is, is like the function is out of scope, for let the function fire i have to put it on another UC that include the UC where is the radio button, but in this uc i haven't the rbtTipo
@user3066925, What is UC? Is your primary problem which you asked is solved? Are you facing new problem?
|
1

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.