简体   繁体   中英

How to correctly handle a call function in a function with javascript

I'm trying to understand how to handle some events with javascript, in particular if a specific event is triggered the current function calls another function. At onLoad the javascript creates the first select the data from the first object. Then if I select the voice with value "Iphone" is called another function for create the second menu with some prices. My main problem is that my solution seems quite ugly, and also when i click more than one time on the button it creates every times the same second select.

 //global objects var product = [ {name: "Samsung"}, {name: "Iphone"}, {name: "Alcatel"}, {name: "Sony"} ] var productPrice = [ {name: "Samsung", price: 190}, {name: "Iphone", price: 290}, {name: "Alcatel", price: 65}, {name: "Sony", price: 330} ] var main = function() { var sel = $('<select>').appendTo('#select-1'); $(product).each(function() { sel.append($("<option>").attr('value', this.name).text(this.name)); }); $('.press').click(function() { if ($("#select-1 option:selected").text() == "Iphone") { handleEvent(); } if ($("#select-1 option:selected").text() != "Iphone") { $("#risposta").remove(); } }); } function handleEvent() { var sel = $('<select>').appendTo('#select-2'); $(productPrice).each(function() { sel.append($("<option>").attr('value', this.name).text(this.price)); }); } $(document).ready(function() { main(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div id="select-1"> </div> <div id="select-2"> </div> <button class="press">Click</button> 

fiddle

Any suggestions?

You question is not 100% clear to me however I gather you want to generate a select tag on dom ready event which you have already. When you select the option iPhone and hit the Click button you want to generate another select tag with the new options from your variable.

First option

Is what I have provided below. Its basically your code but with an extra if statement

jsFiddle link : https://jsfiddle.net/qu9zs2h1/2/

Javascript

//global objects
var product = [{
  name: "Samsung"
}, {
  name: "Iphone"
}, {
  name: "Alcatel"
}, {
  name: "Sony"
}]

var productPrice = [{
  name: "Samsung",
  price: 190
}, {
  name: "Iphone",
  price: 290
}, {
  name: "Alcatel",
  price: 65
}, {
  name: "Sony",
  price: 330
}]

var main = function() {
  var sel = $('<select>').appendTo('#select-1');

  $(product).each(function() {
    sel.append($("<option>").attr('value', this.name).text(this.name));
  });

  $('.press').click(function() {
    if ($("#select-1 option:selected").text() == "Iphone") {
      handleEvent();
    }

    if ($("#select-1 option:selected").text() != "Iphone") {
      $("#risposta").remove();
    }
  });
}

function handleEvent() {
  // We still run the handleEvent function however, if check to see
  // if the second select option has been generated by checking a 
  // class
  if (!$('.prices').length) {
    var sel = $('<select class="prices">').appendTo('#select-2');
    $(productPrice).each(function() {
      sel.append($("<option>").attr('value', this.name).text(this.price));
    });
  }
}

$(document).ready(function() {
  main();
});

Html

<div id="select-1">
</div>
<div id="select-2">
</div>
<button class="press">Click</button>

I had to add the class prices to your second select but that is just a class you can rename which is used to see if that element already exists.

The second option (Probably the better way)

Is you generate everything on dom ready and hide what you want at the start and then simply show elements when you want, like so

jsFiddle : https://jsfiddle.net/qu9zs2h1/5/

Javascript

//global objects
var product = [{
  name: "Samsung"
}, {
  name: "Iphone"
}, {
  name: "Alcatel"
}, {
  name: "Sony"
}]

var productPrice = [{
  name: "Samsung",
  price: 190
}, {
  name: "Iphone",
  price: 290
}, {
  name: "Alcatel",
  price: 65
}, {
  name: "Sony",
  price: 330
}]

// On document ready run this script
$(function() {
    $('.jshidden').removeClass('jshidden').hide();

  var $select1 = $('#select-1');
  var $select2 = $('#select-2');
  var $sel1 = $('<select>').appendTo($select1);
  var $sel2 = $('<select>').appendTo($select2);

  $(product).each(function() {
    $sel1.append($("<option>").attr('value', this.name).text(this.name));
  });

  $(productPrice).each(function() {
    $sel2.append($("<option>").attr('value', this.price).text(this.price));
  });

  $('.press').click(function() {
    if ($select1.find(":selected").text() == "Iphone") {
      $select2.show();
    }

    if ($select1.find(":selected").text() != "Iphone") {
      $select2.hide();
    }
  });
});

Html

<div id="select-1">
</div>
<div id="select-2" class="jshidden">
</div>
<button class="press">Click</button>

PS just a little something, anything which is referring to a variable which is a jQuery element should start with a $ it just helps read the code.

Just a quick update, you can also reduce the .press code with this code

$('.press').click(function() {
  var iPhoneCheck = $select1.find(":selected").text() == "Iphone";
    $select2.toggle(iPhoneCheck);
  });

jQuery.toggle takes in a bool value to either display the target or hide the target

 //global objects var product = [ {name: "Samsung"}, {name: "Iphone"}, {name: "Alcatel"}, {name: "Sony"} ]; var productPrice = [ {name: "Samsung", price: 190}, {name: "Iphone", price: 290}, {name: "Alcatel", price: 65}, {name: "Sony", price: 330} ]; var main = function() { var sel = $('<select>').appendTo('#select-1'); $(product).each(function() { sel.append($("<option>").attr('value', this.name).text(this.name)); }); $('.press').click(function() { var sel2 = document.getElementById('select-2'); var selectedOption = $("#select-1 option:selected").text(); if (selectedOption == "Iphone") { // check if the select was already added if(sel2.children.length == 0) handleEvent(); } else { // I think here you wanted to remove the second select that was added for iphone option sel2.innerHTML = ''; } }); } function handleEvent() { var sel = $('<select>').appendTo('#select-2'); $(productPrice).each(function() { sel.append($("<option>").attr('value', this.name).text(this.price)); }); } // if you already have a declared function, there is no need to an anonymous function // just call the main function directly $(document).ready(main); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div id="select-1"> </div> <div id="select-2"> </div> <button class="press">Click</button> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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