添加图标下一个文本框

时间:2013-10-24 13:40:39

标签: javascript jquery jquery-mobile textbox icons

单击下一个按钮时,如果文本框为空,则控制文本框我想在文本框附近添加删除图标。我尝试了这个,但它没有用。

 $(".nextBtn").click(function () { 
      $(".txt").each(function () {
         $currentVal = $(this).val();
         if ($currentVal.length == 0 || $currentVal == $(this).attr('placeholder')) {
             //$(this).css({"backgroundColor": "black", "color": "white"});
             $('<a data-role="button"data-icon="delete"></a>').insertAfter($(this));
         }                
       });
 });

// jquery mobile

   <div data-role="content">
            <div data-role="fieldcontainer">
                <label for:"name">Name:</label>
                <input type="text" class="txt" id="name" required placeholder="Enter your name">        

            </div>

            <div data-role:"fieldcontainer">
                <label for:"surname">Surname:</label>
                <input type="text" class="txt" id="surname" required placeholder="Enter              your surname">
            </div>  

            <div data-role:"fieldcontainer">
                <label for:"email">E-mail:</label>
                <input type="email" class="email" id="email" required placeholder="Enter your e-mail">
            </div>    
        </div> 

3 个答案:

答案 0 :(得分:0)

.insertAfter(this) = .insertAfter($(this))您错过了这一部分。

答案 1 :(得分:0)

您没有关闭每个条件

$('item').each(function() {
   ...
});

并使用.insertAfter( $(this)),而不是.insertAfter( this );

答案 2 :(得分:0)

http://jsfiddle.net/Xb7Mc/

验证您的HTML

<div data-role="content">
    <div data-role="fieldcontainer">
        <label for="name">Name:</label>
        <input type="text" class="txt" id="name" placeholder="Enter your name" required />
    </div>
    <div data-role="fieldcontainer">
        <label for="surname">Surname:</label>
        <input type="text" class="txt" id="surname" placeholder="Enter your surname" required/>
    </div>
    <div data-role="fieldcontainer">
        <label for="email">E-mail:</label>
        <input type="email" class="email" id="email" placeholder="Enter your e-mail" required/>
    </div>
    <span class="nextBtn">NEXT</span>
</div>

并为您的javascript:

$(".nextBtn").click(function () { 
      $(".txt").each(function () {
         $currentVal = $(this).val();
         // the placeholder will never be the value of an input element
         if ($currentVal.length == 0) {
             //$(this).css({"backgroundColor": "black", "color": "white"});
             $(this).parent().append('<a data-role="button" data-icon="delete">X</a>');
         }
      }); // close the $(".txt").each(function() {              
});