繁体   English   中英

使用jQuery清除输入字段中的值

[英]clear value in an input field with jQuery

嗨,我有许多电话号码输入,它们使用相同的类和相同的显示/隐藏技术。 我希望能够清除输入框中电话号码的内容,即输入名称为input_tel。

但是,似乎清除了所有假设的输入,因为我正在使用以下行; input:text ,当我input:text一个类时,它将无法正常工作。

我的JS和我的一些html在下面或查看jsFiddle

$(".contact_numbers").on('click', '.clearnumber', function () {
  $(this).siblings('input:text').val('');
});

<div class="telephonetwo contact_numbers">
   <input type="text" class="smallinput contactpagelabel" name="faxname" size="10" value="Fax">
   <input type="checkbox" class="contact_no" name="showfax" value="Y">      
   <input type="text" name="fax" size="30" value="016128 13456" class="input_tel">
   <a href="#" class="remove">Hide</a> <a href="#" class="clearnumber">Clear #</a>
 </div>

如果您正在查看我的JSfiddle,请单击显示其他数字以显示清除按钮。

更新资料

我希望能够清除最接近的input_tel而不是全部清除,因为它们是多个数字。

提前致谢

更换:

$(this).siblings('input:text').val('');  

$(this).siblings('input:text').closest('.input_tel').val('');  

JSFIDDLE演示

那么如何定位input_tel类呢?

$(".contact_numbers").on('click', '.clearnumber', function () {
   $(this).parent().parent().find('input.input_tel').val('');
});

假设没有其他输入字段在其上具有那个input_tel类。

这应该做...

$(".contact_numbers").on('click', function () {
  $(".input_tel").val('');
});

用jQuery清除输入字段的安全方法

$.fn.noText = function() {/*use the jquery prototype technology to make a chainable clear field method*/
    if(this.is('input')){ /*check to c if you the element type is an input*/
        this.val('');/*clear the input field*/
    }return this;/*means it is chainable*/
};
$('input').noText();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM