Find Javascript Function (onchange)

Asked

Viewed 46 times

0

I have a question. I want to make a selections box depends on a page using Razor and javascript. TYPE, when selecting the community, the people corresponding to it have to appear. But I can’t call the function with the onchange. Can you help me ? Would there be any other way to do it ? (without using Json)

The code below :

    <script language="JavaScript"></script>


<title></title>

<!--Adiciona as comunidades existentes numa lista-->
@{

    List<String> comunidades = new List<String>();
    conn.Open();
    cmd.CommandText = "SELECT Distinct [nom_Comunidade] FROM [COB].[dbo].[TB_M] 
    dr = cmd.ExecuteReader();

    if (dr.HasRows)
    {
        while (dr.Read())
        {
            comunidades.Add(dr.GetString(0));
        }
    }
    conn.Close();
}


<!--Usa os valores da lista numa caixa de opções-->


<select name="teste" id="teste" onchange="test(this.value);">
    @{ foreach (string rt in comunidades)
        {
            <option>@rt</option>
        }
    }
</select>



<script language="javascript">


              function test1(valor)
            {
                //var thing = document.getElementById("teste").value;
                var thing = valor;

                @{
                    String thing2 = Request["teste"];
                    List<String> all_rt = new List<String>();
                    conn.Open();
                    cmd.CommandText = "SELECT Distinct [nom_pessoa] FROM [COB].[dbo].[TB_M] where [nom_Comunidade] = '" + thing2 +"'" ;
                    dr = cmd.ExecuteReader();

                    if (dr.HasRows)
                    {
                        while (dr.Read())
                        {
                            all_rt.Add(dr.GetString(0));
                        }
                    }
                    conn.Close();

                    foreach (string r in all_rt)
                    {
                        <text>
                        <select>
                            @foreach (string rt in all_rt)
                            {
                                <option>@rt</option>
                              }
                        </select>
                        </text>

                    }

                }

            }


</script>

  • You are calling the function "test", but in the code has a function "Test1", that’s right?

  • The server scripts blocks will run only before the browser response...

  • @Leonardol , the right is the 2 test 1. Ta so pq the last time I went to try a gambiarra haha.

  • @Leandroangelo, how do I make it run only after the answer on the page?

  • Do not, you should create a route for it on the server and fetch the values via Ajax... or you will have to reload the page. I think you’ll have to rethink your entire solution and study a little bit about the platform you’re using.

  • or.... you populate a javascript object with all the content at once and interact with it...

  • @Leandroangelo Da to make the ajax talk to the Azor? And another doubt, the object I would have to create in a js file apart or I can create it in the cshtml own page?

  • Dude, you can do whatever you want and whatever you want, but that doesn’t mean it’s a good option and especially if it should be done. My advice is that you take a step back, study harder and understand what you’re doing.

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.