0

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

 $(".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手机

   <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> 
4

3 回答 3

0

您不会关闭每个条件

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

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

于 2013-10-24T13:54:09.173 回答
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() {              
});
于 2013-10-24T13:58:14.287 回答
0

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

于 2013-10-24T13:51:13.097 回答