Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 触发按$单击附加元素。每个_Jquery_Html - Fatal编程技术网

Jquery 触发按$单击附加元素。每个

Jquery 触发按$单击附加元素。每个,jquery,html,Jquery,Html,我试图用这段代码将选项附加到HTML中现有的select HTML: jQuery: $(".booking-calendar-wrapper-in .month-title").each(function() { var month = $(this).text(); $("#booking-year-month").append("<option>" + month + "</option>"); }); $(“.booking calendar

我试图用这段代码将选项附加到HTML中现有的select

HTML:


jQuery:

$(".booking-calendar-wrapper-in .month-title").each(function() {
    var month = $(this).text();
    $("#booking-year-month").append("<option>" + month + "</option>");
});
$(“.booking calendar wrapper in.month title”)。每个(函数(){
var month=$(this.text();
$(“#预订年-月”)。追加(“+月+”);
});
我还想在用户单击该选项时执行一些操作。我尝试在od之外添加.on.trigger的点击功能,但它不起作用


有什么好的解决方案吗?

您可以在change handler上使用,但在select元素上使用,我假设该元素的id为#booking year month

$('body').on("change" , "#booking-year-month" , function() {
    console.log("selected option: " + this.value);
});

您可以在change handler上使用,但在select元素上使用,我假设该元素的id=#booking year month

$('body').on("change" , "#booking-year-month" , function() {
    console.log("selected option: " + this.value);
});

您可以简单地将onchange方法放在您选择的htmly标记上

<select id="booking-year-month" onchange="YourNewMethod()"></select>


除了在select元素中添加选项之外,还可以在如下选项中添加value属性

$(".booking-calendar-wrapper-in .month-title").each(function() {
  var month = $(this).text();
  $("#booking-year-month").append("<option value='" + month + "'>" + month + "</option>");
});
$(“.booking calendar wrapper in.month title”)。每个(函数(){
var month=$(this.text();
$(“#预订年-月”)。追加(“+月+”);
});

您可以简单地将onchange方法放在您选择的htmly标记上

<select id="booking-year-month" onchange="YourNewMethod()"></select>


除了在select元素中添加选项之外,还可以在如下选项中添加value属性

$(".booking-calendar-wrapper-in .month-title").each(function() {
  var month = $(this).text();
  $("#booking-year-month").append("<option value='" + month + "'>" + month + "</option>");
});
$(“.booking calendar wrapper in.month title”)。每个(函数(){
var month=$(this.text();
$(“#预订年-月”)。追加(“+月+”);
});

您能将html添加到问题中吗?您能将html添加到问题中吗?非常有效!我在这个问题上用了大约1个小时。非常感谢。工作得很好!我在这个问题上用了大约1个小时。非常感谢。