获得同一个div中的同级值 [英] get value of sibling in same div

查看:197
本文介绍了获得同一个div中的同级值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对具有给定类的每个项目执行某种逻辑(代码被重用,这就是为什么我不使用id的原因):这些项目在每个地方都在同一个div中:

I would like to do a certain kind of logic for every item with a given class (the code is reused that why i dont work with id): the items are within the same div everywhere:

<div id="mDiv>
<input type="text" class = "class1>
<input type="hidden"/>
</div>

脚本是这样的:

    $(".class1").val(calcDefaultDate())

    function calcDefaultDate(){
        var currDate = $(this).siblings('input[type=hidden]').val();
        if (currDate == "") {
            return new Date();
        }
        return currDate;
    }

问题是这样,$(this)是Window而不是class1的元素,我对javascript很陌生,所以它可能确实很简单,但应该如何处理?

The problem is that this way the $(this) is the Window instead of the element with class1, I am quite new to javascript so it might be something really easy but how should it be handled?

谢谢!

推荐答案

您非常亲密,您只需要传递函数引用即可

You are very close, You just need to pass the function reference like

$(".class1").val(calcDefaultDate); //Notice removed ()

使用()时会调用函数.

演示

这篇关于获得同一个div中的同级值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆