2014-07-15 53 views
0

如果輸入字段具有必需屬性,是否可以在標籤元素中添加<span class="required">*</span>如何在標籤中添加span.required元素,如果輸入需要屬性?

HTML:

<div class="form-group"> 
    <label class="col-sm-4 control-label" for="customer_phone">Email:</label><p></p> 
    <div class="col-sm-8"> 
     <input type="text" id="b_customer_email" class="form-control" size="40" value="<?php echo get_user_meta (get_current_user_id(),'b_customer_email',true);?>" name="b_customer_email" required> 
    </div> 
</div>` 

而且我已經試過這個jQuery:

$(document).ready(function(){ 
    $("input").has("[required]")function() { 
     $(".control-label").after("<span class='required'>*</span>"); 
    }; 
}); 

span元素標籤元素後不加。

我應該添加到腳本中才能使其工作?

+0

我已經downvoted甚至沒有試圖檢查控制檯的明顯錯誤。 –

+0

夠公平的。 jQuery語法關閉並且有一廂情願的想法 – mplungjan

+0

[.has()](http://api.jquery.com/has/)尋找匹配選擇器的後代元素 - 在你的情況下,重新尋找。你應該使用屬性選擇器'$(「input [required]」)' –

回答

3

Demo。實際上你的方式是正確的。但請記住,:has選擇器不是本地的,因此效率不高。

$(function() { 
    $('.form-group:has(input[required]) > label.control-label') 
     .after('<span class="required">*</span>')  
}) 
+0

我試過你的代碼@Yury Tarabanko,它的工作原理是在標籤元素旁邊添加span元素。 非常感謝你 –

+0

@LizzaFaith不客氣。 –

相關問題