最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Add HTML Attribute without a value using Jquery - Stack Overflow

programmeradmin0浏览0评论

I want to add an attribute to a HTML element without a value using JQuery

<input id="r1" type="radio name="r1" value="1">

Add required

<input id="r1" type="radio name="r1" value="1" required>

How can this be done without the =""after the attribute?

I want to add an attribute to a HTML element without a value using JQuery

<input id="r1" type="radio name="r1" value="1">

Add required

<input id="r1" type="radio name="r1" value="1" required>

How can this be done without the =""after the attribute?

Share Improve this question edited Jun 19, 2013 at 21:53 ojhawkins asked Jun 19, 2013 at 21:43 ojhawkinsojhawkins 3,29816 gold badges53 silver badges69 bronze badges 6
  • 1 don't worry about the ="", it's irrelevant just like the missing / – Kevin B Commented Jun 19, 2013 at 21:47
  • $('input').attr('required',''); – Manish Mishra Commented Jun 19, 2013 at 21:48
  • @KevinB I believe <DOCTYPE !html> would be required, tho. – Ohgodwhy Commented Jun 19, 2013 at 21:48
  • why are there two id attributes – pid Commented Jun 19, 2013 at 21:51
  • it's actually a boolean property and as such not required to have any value. You could add required='fred' and it would work the same. See :html5doctor./html5-forms-introduction-and-new-attributes – scrappedcola Commented Jun 19, 2013 at 21:56
 |  Show 1 more ment

1 Answer 1

Reset to default 8

The standard practice would be to set the property to true

$("#r1").prop("required", true);

This actually results in markup that exactly reads

<input id="r1" required>

JSFiddle demo: http://jsfiddle/8SrED/

发布评论

评论列表(0)

  1. 暂无评论