简体   繁体   English

如何使用javascript正确处理函数中的调用函数

[英]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. 我试图了解如何使用javascript处理某些事件,特别是如果触发了特定事件,则当前函数调用另一个函数。 At onLoad the javascript creates the first select the data from the first object. 在onLoad上,javascript创建第一个从第一个对象中选择数据。 Then if I select the voice with value "Iphone" is called another function for create the second menu with some prices. 然后,如果我选择值为“Iphone”的语音被称为另一个功能,用于创建具有一些价格的第二个菜单。 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. 您的问题并非100%清楚,但我收集您想要在dom ready事件上生成一个select标签。 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. 当您选择iPhone选项并点击“ Click按钮时,您希望使用变量中的新选项生成另一个select标记。

First option 第一种选择

Is what I have provided below. 是我在下面提供的。 Its basically your code but with an extra if statement 它基本上是你的代码,但有一个额外的if语句

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

Javascript 使用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 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. 我必须将类prices添加到第二个select但这只是一个可以重命名的类,用于查看该元素是否已存在。

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 你是否已准备好dom并在开始时隐藏你想要的东西,然后只是在你想要的时候show元素,就像这样

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

Javascript 使用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 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. PS只是一些东西,任何指向变量jQuery元素的东西应该以$开头,它只是帮助读取代码。

Just a quick update, you can also reduce the .press code with this code 只需快速更新,您还可以使用此代码减少.press代码

$('.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 jQuery.toggle接受bool值来显示目标或隐藏目标

 //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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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