Jquery:相对于另一个元素显示元素

发布于 2024-08-25 17:51:34 字数 1367 浏览 3 评论 0原文

我试图在每次用户单击特定表单字段时使用 jquery 显示一个下拉列表...

目前 HTML 是这样的

// Form field to enter the time an event starts
<div>
  <label for="eventTime"> Event Time </label>
    <input type = "text" name="eventTime" id="eventTime" class="time">
  </label>
</div>

// Form field to enter the time an event finishes
<div>
  <label for="eventEnd"> Event Ends</label>
    <input type = "text" name="eventEnds" id="eventEnds" class="time">
  </label>
</div>

并且 Jquery 看起来像这样

    // Display Time Picker  
$('.time').click(function(){
    var thisTime = $(this);


        var timePicker = '<ul class="timePicker">'
            timePicker += '<li>10.00am</li>'
            timePicker += '<li>11.00am</li>'
            timePicker += '<li>12.00am</li>'
            timePicker += '</ul>'

    $('.timePicker').hide().insertAfter(thisTime);

      //show the menu directly over the placeholder
        var pos = thisTime.offset();

            $(".timePicker").attr({
              top: pos.top
              left: pos.left
            });
             $(".timePicker").show();
                });

但是,当我单击一个具有类时间的表单字段时, timePicker 下拉菜单出现,但它总是出现在同一位置,而不是像我希望的那样出现在表单字段旁边。

有什么想法吗?

谢谢

I am trying to display a drop down list using jquery everytime a user clicks on a particular formfield...

At the moment the HTML is as stands

// Form field to enter the time an event starts
<div>
  <label for="eventTime"> Event Time </label>
    <input type = "text" name="eventTime" id="eventTime" class="time">
  </label>
</div>

// Form field to enter the time an event finishes
<div>
  <label for="eventEnd"> Event Ends</label>
    <input type = "text" name="eventEnds" id="eventEnds" class="time">
  </label>
</div>

And the Jquery looks like so

    // Display Time Picker  
$('.time').click(function(){
    var thisTime = $(this);


        var timePicker = '<ul class="timePicker">'
            timePicker += '<li>10.00am</li>'
            timePicker += '<li>11.00am</li>'
            timePicker += '<li>12.00am</li>'
            timePicker += '</ul>'

    $('.timePicker').hide().insertAfter(thisTime);

      //show the menu directly over the placeholder
        var pos = thisTime.offset();

            $(".timePicker").attr({
              top: pos.top
              left: pos.left
            });
             $(".timePicker").show();
                });

However, when I click on one the form fields with class time, the timePicker drop down appears, but it always appears in the same location, not next to the form field as I would like.

Any ideas?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心头的小情儿 2024-09-01 17:51:34

尝试将相对位置应用于父元素,将绝对位置应用于子元素,那么就不需要使用 pos.left 和 pos.top 可能 0,0 就可以了。

try applying relative position to the parent element and absolute position to the child, then there's no need to use pos.left and pos.top maybe 0,0 its fine.

苄①跕圉湢 2024-09-01 17:51:34

您是否考虑过时间选择器插件 我确信还有更多,但是重新发明已经存在的东西是一种耻辱

have you considered a time picker plugin I'm sure there are more but it sees a shame to re-invent something that already exists

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文