【问题标题】:How to remove a specific text in the input value (wordpress)?如何删除输入值中的特定文本(wordpress)?
【发布时间】:2021-06-01 04:52:58
【问题描述】:

我需要删除文本 Private: in value。
我尝试了以下代码,但它没有删除文本。
请告诉我如何删除它?

现有代码(使用插件创建):

<form method="post" class="dwqa-content-edit-form">
     <p>
          <input type="text" name="question_title" value="Private: about your question" tabindex="1">
     </p>
</form>

我尝试过的代码:

jQuery('.dwqa-content-edit-form p input').text(function () {
    return jQuery(this).text().replace(/Private: /g, '');
});

jQuery('.dwqa-content-edit-form p input[type=text]').each(function() {
        if (jQuery(this).val() === "Private: ") {
            jQuery(this).remove();
        }
    });

谢谢。

【问题讨论】:

标签: jquery wordpress input


【解决方案1】:

您可以使用 val 函数获取输入字段的当前值,然后再次使用它来应用修改后的值,而无需“Private:”字符串。下面的例子。

jQuery('.dwqa-content-edit-form p input[type=text]').each(function() {
        console.log($(this).val());
        var value = $(this).val().replace("Private: ", '');
        $(this).val(value);
 });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>

<form method="post" class="dwqa-content-edit-form">
     <p>
          <input type="text" name="question_title" value="Private: about your question" tabindex="1">
     </p>
</form>

【讨论】:

  • 嗨 iismaell:哦,它工作得很好,非常感谢您的帮助 iismaell :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-08-19
  • 2018-11-25
  • 2013-12-11
  • 2023-03-12
  • 1970-01-01
  • 2014-08-21
  • 1970-01-01
相关资源
最近更新 更多