jQuery1.6.3下attr(required)获取值实例页面

回到相关文章 »

代码:

CSS代码:
.demo pre{display:inline; margin:0;}
.co{color:#f30;}
.demo p{margin-top:2px;}
                
HTML代码:
<div class="demo">
    <pre>&lt;input id="input1" required /></pre> → <input id="input1" required />
    <p><code>$("#input1").attr("required")</code> → <span id="result1" class="co"></span></p>
    <pre>&lt;input id="input2" /></pre> → <input id="input2" />
    <p><code>$("#input2").attr("required")</code> → <span id="result2" class="co"></span></p>
</div>
                
JS代码:
$.each([1,2], function(i, j) {
    var required = $("#input" + j).attr("required");
    if (required === "") {
        required = "空字符串";
    }
    $("#result" + j).html(required + "");
});
                

效果:

<input id="input1" required />

$("#input1").attr("required")

<input id="input2" />

$("#input2").attr("required")