如果我选中的国家/地区处于非活动状态,我希望将其设置为活动状态.但我不知道怎么做,这是我的代码.

PS:我现在用的是手机,所以这个片段是没有用的.对于那些通过代码片段提取我的代码的人来说,我非常感激.

这是一个例子,因为唯一的问题是如果单击相应的元素或div,如何创建任何函数.例如,如果我选中第二格中的复选框,则只有第二格中发生了一些事情.然后如果我没有判断其他div,它什么也没有发生或影响.

const Countries = ["Philippines", "New York", "Singapore"];

function createList() {
  const liTag = $('.Handler');
  for (let i = 0; i < Countries.length; i++) {
    const List = `
      <ul id="CountryList">
       <li>
          <div class="NameOfCountry">
            <input type="checkbox" class="Check">
            <p class="CountryName">Hello</p>
            <p class="InActive">Inactive</p>
          </div>
        </li>
      </ul>   
    `;
    
    var elem = $(List);
    elem.find('.CountryName').html(Countries[i]);
    elem.find('.Check').change(function() {
      //Create a code that if the checkbox is Check, the word inactive makes Active on Respective Element. 
    })
    liTag.append(elem);
  }
}
createList();
* {
  margin: 0;
  padding: 0;
  outline: none;
  box-sizing: border-box
}

body {
  background: #40fff5;
}

.Handler {
  width: 100%;
  padding: 12px;
}

ul li {
  list-style: none;
}

.NameOfCountry {
  background: blue;
  width: 100%;
  display: flex;
  justify-content: space-between;
  gap: 6px;
  border-radius: 8px;
  align-items: center;
  padding: 8px;
  margin-top: 6px;
}

.CountryName {
  color: white;
  font-size: 18px;
  font-weight: bold;
  width: 100%;
  text-align: center;
}

.InActive {
  background: white;
  border-radius: 5px;
  color: red;
  font-size: 11px;
  font-weight: bold;
  width: 30%;
  padding: 5px;
  text-align: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="Handler"></div>

推荐答案

我补充了几件事 将status类添加到您的

标牌 在你的.change功能中增加了一些功能,可以在"活动"和"非活动"之间切换,并根据复选框状态改变背景 colored颜色 .

const Countries = ["Philippines", "New York", "Singapore"];

function createList() {
  const liTag = $('.Handler');
  for (let i = 0; i < Countries.length; i++) {
    const List = `
          <ul id="CountryList">
            <li>
              <div class="NameOfCountry">
                <input type="checkbox" class="Check">
                <p class="CountryName">${Countries[i]}</p>
                <p class="Status Inactive">Inactive</p>
              </div>
            </li>
          </ul>
        `;

    var elem = $(List);
    elem.find('.Check').change(function() {
      const statusElem = $(this).closest('.NameOfCountry').find('.Status');
      statusElem.text($(this).prop('checked') ? 'Active' : 'Inactive');
      statusElem.toggleClass('Active Inactive');
    });
    liTag.append(elem);
  }
}

createList();
* {
  margin: 0;
  padding: 0;
  outline: none;
  box-sizing: border-box;
}

body {
  background: #40fff5;
}

.Handler {
  width: 100%;
  padding: 12px;
}

ul li {
  list-style: none;
}

.NameOfCountry {
  background: blue;
  width: 100%;
  display: flex;
  justify-content: space-between;
  gap: 6px;
  border-radius: 8px;
  align-items: center;
  padding: 8px;
  margin-top: 6px;
}

.CountryName {
  color: white;
  font-size: 18px;
  font-weight: bold;
  width: 100%;
  text-align: center;
}

.Active {
  background: green;
  border-radius: 5px;
  color: white;
  font-size: 11px;
  font-weight: bold;
  width: 30%;
  padding: 5px;
  text-align: center;
}

.Inactive {
  background: white;
  border-radius: 5px;
  color: red;
  font-size: 11px;
  font-weight: bold;
  width: 30%;
  padding: 5px;
  text-align: center;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="Handler"></div>

Javascript相关问答推荐

使用redux-toolet DelivercThunks刷新访问令牌

vanillajs-datepicker未设置输入值,日期单击时未触发更改事件

Phaser框架-将子对象附加到Actor

如何在RTK上设置轮询,每24小时

如何用显示网格平滑地将元素从一个地方移动到另一个地方?

InDesign—创建一个独立的窗口,在文档中进行更正时保持打开状态

Snowflake JavaScript存储过程返回成功,尽管预期失败

Msgraph用户邀请自定义邮箱模板

函数返回与输入对象具有相同键的对象

将内容大小设置为剩余可用空间,但如果需要,可在div上显示滚动条

Next.js中的服务器端组件列表筛选

将基元传递给THEN处理程序

删除元素属性或样式属性的首选方法

我怎样才能点击一个元素,并获得一个与 puppeteer 师导航页面的URL?

需要RTK-在ReactJS中查询多个组件的Mutations 数据

如何在Web项目中同步语音合成和文本 colored颜色 更改

我想为我的Reaction项目在画布上加载图像/视频,图像正在工作,但视频没有

如何在单击链接时设置一个JavaScript变量(以打开弹出窗口的特定部分)

:host::ng-Deep不将样式应用于material 复选框

从客户端更新MongoDB数据库