jQuery单击隐藏下一个添加的元素

时间:2018-11-24 12:40:12

标签: javascript php jquery html

我有一个按钮,每次单击都会显示下拉菜单元素。 现在,我需要在下拉菜单上方添加复选框-我已经完成了,但是此复选框元素应该仅首次出现。

enter image description here

问题是,每次点击时,我都获得了与以下复选框相关联的两个下拉列表。

enter image description here

我只需要在第一次单击按钮以及下拉菜单中使用复选框。 因此,第二次单击按钮时,我试图隐藏该复选框,但是上一个复选框被隐藏了。

public function getPreferenceKeysAction() {
....
<div style="margin:2px 0px 15px 0px;">
    <input id = "mydd" type="checkbox" name="vehicle1" value="RegisteredUsers"> Registered Users</input><br>
    <input id = "mydd" type="checkbox" name="vehicle1" value="RegisteredUsers"> Opted Users</input><br>
}

在JS中

$(add_button).click(function(e){ //on add input button click
        if($("#mydd").length){
            alert("The element you're testing is present.");
            $("#mydd").hide();
        }
        else{
            alert("NOt present.");

        }
});

2 个答案:

答案 0 :(得分:0)

为什么不将类添加到元素或数据属性。

使用jQuery都很容易实现。 如果要使用类,只需将数据方法更改为hasClass和addClass

只要做一个

java.lang.IllegalArgumentException: Property must not be null!
at org.springframework.util.Assert.notNull(Assert.java:134)
at com.wallet.document.PlayAndEarnProgram_Accessor_71zczn.getProperty(Unknown Source)
at org.springframework.data.mongodb.repository.query.DtoInstantiatingConverter$2.doWithPersistentProperty(DtoInstantiatingConverter.java:102)
at org.springframework.data.mapping.model.BasicPersistentEntity.doWithProperties(BasicPersistentEntity.java:346)
at org.springframework.data.mongodb.repository.query.DtoInstantiatingConverter.convert(DtoInstantiatingConverter.java:92)
at org.springframework.data.repository.query.ResultProcessor$ChainingConverter.convert(ResultProcessor.java:212)
at org.springframework.data.repository.query.ResultProcessor$ChainingConverter$1.convert(ResultProcessor.java:200)
at org.springframework.data.repository.query.ResultProcessor$ChainingConverter.convert(ResultProcessor.java:212)
at org.springframework.data.repository.query.ResultProcessor.processResult(ResultProcessor.java:149)
at org.springframework.data.mongodb.repository.query.MongoQueryExecution$ResultProcessingConverter.convert(MongoQueryExecution.java:444)
at org.springframework.data.mongodb.repository.query.MongoQueryExecution$ResultProcessingExecution.execute(MongoQueryExecution.java:411)
at org.springframework.data.mongodb.repository.query.AbstractMongoQuery.execute(AbstractMongoQuery.java:94)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:483)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:461)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:56)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:57)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy5009.getActiveVoucherByplayAndEarnId(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

答案 1 :(得分:0)

将您的id =“ mydd”更改为class =“ mydd”,您应该使用具有唯一值的id,这样做可以隐藏跳过其中2个元素的元素。

$('.mydd').slice(2).hide();

如果要隐藏包装复选框的div,请添加.parent()

$('.mydd').parent().slice(1).hide();