简体   繁体   English

使用jQuery清除输入文本字段值

[英]Clearing input text field value with Jquery

I seem to be able to hide the resource container using 我似乎能够使用隐藏资源容器

resource.parent().parent().hide(); 

but I don't understand why the input value is not clearing with 但我不明白为什么输入值不能用

resource.parent().siblings('.resource-value').children('input').val('');

when I use 当我使用

resource.parent().siblings('.resource-value') I get the parent of the input value but adding .children('input').val('') on top of that does nothing or if I add .children('input:text').val('') resource.parent().siblings('.resource-value')我得到输入值的父级,但在此之上添加.children('input').val('')则无济于事,或者如果我添加.children('input:text').val('')

I have very similar code for something else which works just fine, looked at other questions and not sure what I'm missing. 对于其他工作正常的代码,我也有非常相似的代码,查看了其他问题,不确定我缺少什么。

 function removeResource(resource) { 'use strict'; //hide resource on screen resource.parent().parent().hide(); //set resource text value to '' resource.parent().siblings('.resource-value').children('input').val(''); } (function($) { 'use strict'; $(function() { $('#resources').on('click', '.remove-resource', function(evt) { // Stop the anchor's default behavior evt.preventDefault(); // Remove the image, toggle the anchors removeResource($(this)); }); }); })(jQuery); 
 <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> </head> <body> <div id="resources"> <div class="resource"> <div class="resource-value"> <input type="text" name="resources[]" value="1" /> </div> <p class="hide-if-no-js"><a title="remove resource" href="javascript:;" class="remove-resource">remove resource</a > </p> <!-- .hide-if-no-js --> </div> <div class="resource"> <div class="resource-value"> <input type="text" name="resources[]" value="2"/> </div> <p class="hide-if-no-js"><a title="remove resourcee" href="javascript:;" class="remove-resource">remove resource</a> </p> <!-- .hide-if-no-js --> </div> </div> </body> <html/> 

Try to log your sibling element with Try to change your removeResource function to 尝试使用“尝试将removeResource函数更改为”来记录同级元素

function removeResource(resource) {

  'use strict';

  //hide resource on screen
  var parent = resource.parent().parent();
  parent.hide();

  // log your element
  console.log(parent.find('.resource-value input'));
  // make sure you are getting an element you need
  console.log(parent.siblings('.resource-value').childer('input').get(0);
  //set resource text value to ''
  parent.find('.resource-value input').val('');
}

Tried your code and worked fine for me in terms of the actual value of the field clearing, though in inspector the HTML element still has the value attribute showing. 尝试了您的代码,并在字段清除的实际值方面为我工作得很好,尽管在检查器中HTML元素仍显示value属性。

You can use 您可以使用

.attr('value','')

to clear that too http://jsfiddle.net/bvtg93dm 还要清除http://jsfiddle.net/bvtg93dm

您只需要更改女巫jquery的值即可将其设置为“”(因此为空)。

input.attr('value','')

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

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