【问题标题】:How do I target required inputs with jQuery [duplicate]如何使用 jQuery 定位所需的输入 [重复]
【发布时间】:2013-09-11 18:16:24
【问题描述】:

我有一个表单,其中一些字段是必需的,我如何只针对这些字段?

这是我的代码:

<p><strong>Your Information</strong></p>

<p><label for="name">Name <span style="color:#990000;">*</span></label><br />
<input required type="text" name="name" id="name" class="input" style="width: 250px; margin-left: 0px;"/></p>

<p><label for="brokerOffice">Broker Office <span style="color:#990000;">*</span></label><br />
<input required type="text" name="brokerOffice" id="brokerOffice" class="input" style="width: 250px; margin-left: 0px;"/></p>

<p><label for="email">Email Address <span style="color:#990000;">*</span></label><br />
<input required type="text" name="email" id="email" class="input" style="width: 250px; margin-left: 0px;"/></p>

<p><label for="phone">Phone <span style="color:#990000;">*</span></label><br />
<input required type="text" name="phone" id="phone" class="input" style="width: 250px; margin-left: 0px;"/></p>

您可以使用 attr 选择器定位它们吗?

【问题讨论】:

  • ... 真的吗?问一个是或否的问题,你已经知道答案已经在文档中回答了吗?这就像问我是否可以使用 innerHTML 属性获取 div 的 innerHTML。
  • ... 你发布了无效的 html,并且到处都是双 ID。
  • 我在网站上找不到它...所以我添加了它。 StackOverflow 与大多数文档一样好,甚至更好,许多用户首先来到这里(有意或通过谷歌)寻求答案。
  • 如果你愿意,你可以删除它并取回声望点。

标签: jquery html input required


【解决方案1】:

您可以将“必需”视为一个属性并定位以下任何一种方式:

<script>

    $(document).ready(function() {

        $('input[required]').css("border-color", "red");
        $('input[required="true"]').css("border-color", "red");
        $('input[required="required"]').css("border-color", "red");
        $('input:required').css("border-color", "red");

    });

</script>

【讨论】:

  • 很好的侦探。不要介意反对者 - 能够回答自己的问题总是很棒。
猜你喜欢
  • 1970-01-01
  • 2016-12-23
  • 1970-01-01
  • 2016-01-03
  • 2021-04-22
  • 1970-01-01
  • 1970-01-01
  • 2020-01-16
  • 1970-01-01
相关资源
最近更新 更多