문제

I was trying to set attribute to an input tag using jQuery. All other attributes for input tag like maxlength,name,class worked well. But i'm having trouble with required attribute.

I tried this way

 $("<label><strong>Class"+fare+"</strong></label><input type='text' value='' />")
.attr("class", "nameval") .attr("name", "class"+fare+"") .attr("required","required")

I also tried other ways refering to some forums

.attr("required","")
.attr("required",true)
.attr("required","true")
.prop("required",true)

Please let me know where i was wrong.

도움이 되었습니까?

해결책 2

If you are going to append that HTML, then, why you are using it as a selector?

Just create it directly in the string:

$(form).append("<label><strong>Class"+fare+"</strong></label><input type='text' value='' required='required"/>');

다른 팁

You can try like this.

var element = $("<label><strong>Class"+fare+"</strong></label><input type='text' value='' />")
.attr("class", "nameval") .attr("name", "class"+fare+"") .attr("required","required");

$("#MyFormId").append(element);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top