Query在JavaScript中使用Dropdown未调用的事

Query在JavaScript中使用Dropdown未调用的事

本文介绍了使用JQuery在JavaScript中使用Dropdown未调用的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个下拉列表月和年。根据年份,月份列表将会改变。

这工作正常,但由于某种原因我的span field 没有更新。问题是当用户选择一个月时,点击事件没有被调用。
注意:年份完美。

I have two dropdowns Month and Year. Depending on the year, the list of months will change.This works fine, but for some reason my "span field" is not getting updated. The problem is that the click event is not getting called, when the user choose for a month.Note: The year works perfect.

隐藏下拉列表

function Dropdown (element) {
    $("#month div, #year div").hide(); // hide all dropdowns
    if (element) $("#"+element+" div").toggle(); // show dropdown of specified element
}

根据下拉列表显示列表的逻辑年值

Logic to show the list depending on the dropdown year value

function UpdateMonths(){
var temp = "";
    if (parseInt(Get.Year()) <= 2012){
        temp = "<p>Month1</p>";
    }else{
        temp = "<p>Month9</p>";
    }
    return temp;
}

显示月或年的列表

$("#month span, #year span").click(function(event) {

    $("#month div").html(UpdateMonths()); // IMPORTANT Show the right list of months

    event.stopPropagation();
    if (!$(this).hasClass('disabled')) { // only open downdown when not disabled
        Dropdown($(this).parent().attr("id"));
    } else Dropdown(); // hide all dropdown menues
});

用户选择他想要的值。
不工作几个月

The user choose the value he wants.Not working for months

$("#location div p, #month div p, #year div p").click(function() {

    var value = $(this).text(); // grab new location name/month/year.
    $(this).parent().parent().children("span").text(value); // update selected location name/month/year.
    RequestToServer();
});

月份下拉菜单

<div id="month">
    <span class="input">Januar</span>
    <div class=".dropdown">
    <p></p>
</div>
</div>


推荐答案

选择器应该是:

$("#month, #year").click(function() {

原因是因为您想要在 #month #year divs。通过在原始代码中定位跨距,您只能在span本身上获取点击事件,默认情况下是一个内联元素,因此它不会填充100 %width。

The reason for this is because you want to detect a click on anywhere within the #month and #year divs. By targeting the spans in your original code, you would only get the click event on the span itself, which by default is an inline element and so it doesn't fill the 100% width.

这篇关于使用JQuery在JavaScript中使用Dropdown未调用的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 13:59