如何在HTML中给select字段添加required属性而不使用input标签

3
我有这个性别选择器代码,如何像上面的文本输入字段一样添加必填属性? 性别代码没有输入标签可添加要求的标签名称和ID,以供 PHP 评估。 如何将性别数据作为普通选择字段发送到下一个处理页面。这是一个两周经验的程序员提出的问题。

$('.gender-selector .item').on('click', function() {
  $('.gender-selector .item').removeClass('active');
  $(this).addClass('active')
})
.gender-selector {
  width: 300px;
}

.gender-selector h1,
h2,
h3,
h4,
h5,
h6 {
  text-align: center;
  width: 100%;
  height: 65px;
  align-items: center;
  display: flex;
  justify-content: center;
  font-weight: normal;
}

.gender-selector .container {
  width: 100%;
  display: flex;
  justify-content: space-between;
}

.gender-selector .container .item {
  width: 140px;
  height: 150px;
  border: 2px solid #eceff1;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  position: relative;
  background: #fff;
  border-radius: 8px;
  box-shadow: inset 0px 0px 0px #000 42;
  transition: all linear 0.1s;
}

.gender-selector .container .item.girl.active {
  border: 2px solid #e91e63;
}

.gender-selector .container .item.girl.active .circle {
  width: 15px;
  height: 15px;
  background: #e91e63;
}

.gender-selector .container .item.girl:hover {
  cursor: pointer;
  border: 2px solid #e91e63;
}

.gender-selector .container .item.boy.active {
  border: 2px solid #2196f3;
}

.gender-selector .container .item.boy.active .circle {
  width: 15px;
  height: 15px;
  background: #2196f3;
}

.gender-selector .container .item.boy:hover {
  cursor: pointer;
  border: 2px solid #2196f3;
}

.gender-selector .container .item .icon {
  width: 64px;
  height: 64px;
  overflow: hidden;
  margin: 0 auto;
}

.gender-selector .container .item .icon img {
  max-width: 100%;
  max-height: 100%;
}

.gender-selector .container .item p {
  text-align: center;
  margin-top: 10px;
}

.gender-selector .container .item .checked {
  width: 20px;
  height: 20px;
  background: #eceff1;
  border-radius: 50%;
  position: absolute;
  top: 5px;
  right: 5px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.gender-selector .container .item .checked .circle {
  width: 0px;
  height: 0px;
  background: #e91e63;
  border-radius: 50%;
  transition: all linear 0.1s;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form>
  <input type="text" name="name" id="name" placeholder="My name is" required>
  <div class="gender-selector center">
    <h3>I am</h3>
    <div class="container">
      <div class="item girl">
        <div class="checked">
          <div class="circle"></div>
        </div>
        <div class="icon">
          <img src="https://image.flaticon.com/icons/svg/40/40739.svg" alt="">
        </div>
        <p>Female</p>
      </div>
      <div class="item boy">
        <div class="checked">
          <div class="circle"></div>
        </div>
        <div class="icon">
          <img src="https://image.flaticon.com/icons/svg/40/40541.svg" alt="">
        </div>
        <p>Male</p>
      </div>
    </div>
  </div>
  <input type="submit"></input>
</form>


你想要一种没有输入标签的验证解决方案吗? - Always Helping
1个回答

2

您的代码缺少每个选项的输入。该输入必须是radio类型,并且应该具有相同的名称,但具有不同的值, 如下所示:

<label>
    <input type="radio" required="required" value="boy"> Boy
</label>

<label>
     <input type="radio" required="required" value="girl"> Girl
</label>

你可以将它们放在与div.circle相同的位置,并将其样式设置为与您现有的样式相同。
如果您想使输入标签隐藏,只需这样做,在JS中添加选择的内容,当用户单击它们时,将所选内容添加到用户单击的内容中。
请注意,如果输入被隐藏,您将无法在用户单击它之前提交表单,并且用户不会知道表单未提交的原因,因为您会有一个控制台错误。

谢谢,但是如果表单提交时未选择名称为“gender”的输入,则有没有一种手动显示错误的方法来触发代码以手动显示错误? - Avye Le
@AvyeLe 当然可以,因为你正在使用jquery,所以你可以轻松地添加一个prevent default到该表单提交,并在实际提交之前验证所有数据并添加自己的验证错误。 - marcogmonteiro

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接