Home > Blockchain >  When the first city is selected, the first county comes automatically. I want the incoming county to
When the first city is selected, the first county comes automatically. I want the incoming county to

Time:01-30

We have 2 drowboxlists. They are linked together in the database. I want that when City is selected, the town is empty and the user selects the town. When the city is selected in my code that works now, the towns comes automatically.

$('#ikametSehir').on("click", function () {

var x = document.getElementById("ikametIlce");

$("#ikametIlce").empty();

var a = $('#ikametSehir :selected').text();

$.ajax

({

               type: "GET",

               url: "/Home/getIlce",

               data: { "sehir": $('#ikametSehir :selected').text() },

               success: function (Ilce) {

                   var aa = Ilce;

                   var uzunluk = Ilce.length;

                   for (var i = 0; i < uzunluk; i  ) {

                       var option = document.createElement("option");

                       option.text = Ilce[i];

                       x.add(option);

                   }


           });

   });

CodePudding user response:

I add first option with ;

var option = document.createElement("option");

        option.text = "İlçe Seçiniz";

        x.add(option);

but problem if user choose "ilçe Seçiniz" I add validation this ;

if ('#ikametSehir :selected') {

            if ('#ikametIlce :selected', "İlçe Seçiniz") {

                $('#ikametIlce').focus()

                alert('Lütfen İlçe Seçiniz')

                return false;

        }

    }

CodePudding user response:

Here's how you can modify your code to have the county dropdownlist be empty when a city is selected:

javascript Copy code

$('#ikametSehir').on("click", function () {
  var x = document.getElementById("ikametIlce");
  $("#ikametIlce").empty();
  var a = $('#ikametSehir :selected').text();
  // your code to populate the county dropdownlist based on the selected city
});

This code listens for a click event on the ikametSehir dropdownlist and clears the ikametIlce dropdownlist every time the city is selected.

  • Related