简体   繁体   English

使用Jquery添加没有值的HTML属性

[英]Add HTML Attribute without a value using Jquery

I want to add an attribute to a HTML element without a value using JQuery 我想使用JQuery向没有值的HTML元素添加属性

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

Add required 添加required

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

How can this be done without the ="" after the attribute? 如果没有属性后面的=""怎么办呢?

The standard practice would be to set the property to true 标准做法是将属性设置为true

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

This actually results in markup that exactly reads 这实际上会导致标记完全读取

<input id="r1" required>

JSFiddle demo: http://jsfiddle.net/8SrED/ JSFiddle演示: http//jsfiddle.net/8SrED/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM