如何使用 jquery 删除输入字段后的文本?

How to remove the text after input field using jquery?

如何删除jQuery中输入后的文字?

jQuery

<div id="date_filter_si">
    <input type="radio" class="date_action" name="date_action" value="Y" checked=""> year
    <input type="radio" class="date_action" name="date_action" value="W" checked=""> Weekly
</div>

预期输出

<div id="date_filter_si">
    <input type="radio" class="date_action" name="date_action" value="Y" checked=""> 
    <input type="radio" class="date_action" name="date_action" value="W" checked=""> 
</div>
$( "#date_filter_si:contains('Weekly')" ).remove();

您可以过滤文本节点(element.nodeType == 3),然后删除。

$("#date_filter_si").contents().filter(function() {
    return this.nodeType === 3; 
}).remove();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="date_filter_si">  
    <input type="radio" class="date_action" name="date_action" value="Y" checked="" onclick="set_date_filter('Y')"> year
    <input type="radio" class="date_action" name="date_action" value="W" checked="" onclick="set_date_filter('W')"> Weekly
</div>

您可以使用 Node.nextSibling 属性 更改元素的下一个同级文本。

$("#date_filter_si > input")[0].nextSibling.textContent = "";

$("#date_filter_si > input")[0].nextSibling.textContent = "";
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="date_filter_si">
  <input type="radio" class="date_action"> year
</div>

如果您有多个输入和同级文本,请使用:

$("#date_filter_si > input").each(function(){
    this.nextSibling.textContent = "";
});

$("#date_filter_si > input").each(function(){
  this.nextSibling.textContent = "";
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="date_filter_si">
  <input type="radio" class="date_action"> year
  <input type="radio" class="date_action"> week
</div>

我就是这样做的。

$( "#date_filter_si:contains('Weekly') input" ).each(function(){
  var el = this.nextSibling;
  if(el && el.nodeType === Node.TEXT_NODE) {
    el.parentNode.removeChild(el);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="date_filter_si">
    <input type="radio" class="date_action" name="date_action" value="Y" checked="" onclick="set_date_filter('Y')"> year
    <input type="radio" class="date_action" name="date_action" value="W" checked="" onclick="set_date_filter('W')"> Weekly
</div>