gpt4 book ai didi

javascript - 如何使用 jQuery 制作一个必需的选择框?

转载 作者:行者123 更新时间:2023-12-03 11:08:36 25 4
gpt4 key购买 nike

记住这一点

$("div input[id = 'weight']").attr('required','required');

如何将它用于选择框#cargo以使其成为必需?

最佳答案

是的,可以将 required 属性添加到 select 元素,只要第一个 option 的值为 "",一个空字符串。

注意: 必需 不支持< IE9

示例:

$('select#required').attr('required',1);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<form>

<select id="required">
<option value="">Please select</option>
<option value="one">One</option>
<option value="two">Two</option>
<option value="three">Three</option>
</select>

<input type="submit" value="Submit" />

</form>

文档:(粗体文本很重要,但不在文档中)

http://dev.w3.org/html5/spec-author-view/the-select-element.html#the-select-element

The required attribute is a boolean attribute.
When specified, the user will be required to select a value before submitting the form.

If a select element

  • has a required attribute specified,
  • does not have a multiple attribute specified,
  • and has a display size of 1;
  • and if the value of the first option element in the select element's list of options (if any) is the empty string,
  • and that option element's parent node is the select element (and not an optgroup element),

then that option is the select element's placeholder label option.

关于javascript - 如何使用 jQuery 制作一个必需的选择框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27714968/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com