如何使用jQuery定位所需的输入

时间:2013-09-11 18:16:25

标签: jquery html5 input required

我有一个表单,有些字段是必需的,我如何只定位那些?

这是我的代码:

<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选择器定位它们吗?

1 个答案:

答案 0 :(得分:4)

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

<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>