English 中文(简体)
如何在不增加方法或类别的情况下将所有投入的面积缩小?
原标题:How can trim spaces in all inputs without adding methods or classes?

I m 试图从投入的开始和结束中删除空白处,而不增加一个类别或id或事件

我尝试了live demo,但正在使用change activity

<javascript>
  function trim(el) {
    el.value = el.value.
    replace(/(^s*)|(s*$)/gi, ""). // removes leading and trailing spaces
    replace(/[ ]{2,}/gi, " "). // replaces multiple spaces with one space 
    replace(/
 +/, "
"); // Removes spaces after newlines
    return;
  }
</script>
<p>Search1: <input type="text" onchange="return trim(this)" /></p>
<p>Search2: <input type="text" onchange="return trim(this)" /></p>
<p>Search3: <input type="text" onchange="return trim(this)" /></p>
<p>Search4: <input type="text" onchange="return trim(this)" /></p>
<p>Search5: <input type="text" onchange="return trim(this)" /></p>

有些人可以帮助我,如何把我的所有投入价值(CSS OR JAVASCRIPT)当作:

 <script>
   Here in this script will trim blank spaces starting or ending so don t need to add anything in the input
 </script> 
 <input type="text" />

我试图这样做,但没有工作

 $(.input).text().trim()

有些人可以帮助我吗?

提前感谢。

最佳回答

>。 2. 对类型案文的投入:-

http://www.ohchr.org。

$(function(){
    $( input[type="text"] ).change(function(){
        this.value = $.trim(this.value);
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<p>Search1: <input type="text"/></p>
<p>Search2: <input type="text"/></p>
<p>Search3: <input type="text"/></p>
<p>Search4: <input type="text"/></p>
<p>Search5: <input type="text"/></p>

<><0>

window.onload = function() {
  var inputs = document.getElementsByTagName( input );
  for (var i = 0; i < inputs.length; i++) {
    if (inputs[i].type ==  text ) {
      inputs[i].onchange = function() {
        this.value = this.value.replace(/^s+/,   ).replace(/s+$/,   );
      };
    }
  }
}
<p>Search1: <input type="text"/></p>
<p>Search2: <input type="text"/></p>
<p>Search3: <input type="text"/></p>
<p>Search4: <input type="text"/></p>
<p>Search5: <input type="text"/></p>
问题回答

This is one of the easiest way i found :), it uses jquery hope it helps, it uses your function The input have to be only:

<input type="text" name="name">

这自动改变所有投入,或者你可以调往某类。

function trim_text(el) {
    el.value = el.value.
    replace(/(^s*)|(s*$)/gi, ""). // removes leading and trailing spaces
    replace(/[ ]{2,}/gi, " "). // replaces multiple spaces with one space
    replace(/
 +/, "
"); // Removes spaces after newlines
    return;
}
$(function(){
  $("textarea").change(function() {
    trim_text(this);
  });

  $("input").change(function() {
    trim_text(this);
  });
});

引证:

function trim(el) {
    var trimmedValue = el.val().trim();
    el.val(trimmedValue);
}

Your syntax for .trim() is wrong. To trim a input s value you don t need any regex, just try to change this:

$(.input).text().trim();

为此:

$( input ).val().trim();

投入不是一类,因此,你不必向它提出。 然后,你必须把它列入在内。 投入的价值以阀(阀)法而不是(文本)给出。

这只会贬低投入的价值,因此,如上所述,你必须在一项职能中加以利用,以便适当开展工作。

这是使用<条码>(波茨雷克5.3<>/条码>的星号。

window.onload = function () {
  const controls = document.querySelectorAll( .form-control );
  controls.forEach((el) => {
    el.onchange = function () {
      this.value = this.value.replace(/^s+/,   ).replace(/s+$/,   );
    };
  })
};




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签