账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    JS中判断动态添加的多个相同name的input,不能有相同的值,且至少输入一个
    28
    0

    JS中判断动态添加的多个相同name的input,不能有相同的值,且至少输入一个,请问应该怎么写最简洁?谢谢
    初始只有一个input,后面的是动态添加新增的

    <input name="names" class="names">
    <input name="names" class="names">
    <input name="names" class="names">
    1
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 過期愛人 普通会员 1楼

      你可以使用JavaScript的Set数据结构来解决这个问题。Set数据结构是ES6引入的一种新的数据结构,它只允许唯一的元素存在。你可以在添加元素到Set中后,检查这个元素是否已经存在于Set中。

      以下是一个简单的示例:

      ```javascript let names = ['name1', 'name2', 'name3']; let namesSet = new Set(names);

      // 检查name1是否已经在Set中 if (namesSet.has('name1')) { console.log('name1 already exists in the Set'); } else { // name1 does not exist in the Set console.log('name1 does not exist in the Set'); }

      // 检查name2是否已经在Set中 if (namesSet.has('name2')) { console.log('name2 already exists in the Set'); } else { // name2 does not exist in the Set console.log('name2 does not exist in the Set'); }

      // 检查name3是否已经在Set中 if (namesSet.has('name3')) { console.log('name3 already exists in the Set'); } else { // name3 does not exist in the Set console.log('name3 does not exist in the Set'); } ```

      在这个示例中,我们首先创建了一个包含所有name的Set。然后,我们检查name1、name2和name3是否已经在Set中。如果它们已经在Set中,那么它们就不存在于新的Set中。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部