如何使用jquery删除空格

时间:2017-12-29 05:26:53

标签: php jquery html

如何删除屏幕截图中的实际发票金额中的空格以及以下情况下显示的代码。

<div class="form-group">
    <label class="col-sm-3 control-label">Actual Invoice Amount</label>
        <div class="col-sm-8">
             <input type="text" name="payment_act_invamt" id="act_invamt" class="form-control act_invamt"  placeholder="Actual Invoice Amount"   required>  
             </div>

enter image description here

4 个答案:

答案 0 :(得分:2)

使用JavaScript trim()方法。此方法将从字符串

的两侧删除空格

function InvoiceAmount(){
    var input_value = document.getElementById("act_invamt").value;
    alert(input_value + "\n" + input_value.trim())
    
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-group">
<label class="col-sm-3 control-label">Actual Invoice Amount</label>
<div class="col-sm-8">
<input type="text" name="payment_act_invamt" id="act_invamt" class="form-control act_invamt"  placeholder="Actual Invoice Amount"   required >

<button onclick="InvoiceAmount()">Click Here</button>
</div>

此处我显示了使用trim方法和不使用trim方法

的区别

答案 1 :(得分:0)

&#13;
&#13;
var formvalue = $('input[name="somename"]').val();

formvalue = formvalue.trim();
    
$('input[name="somename"]').val(formvalue);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<form>
  <input type="text" name="somename" value="    9000">
  
</form>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

//试试这个。我希望它有用..

var act_invamt = document.getElementById("act_invamt").value
var str = act_invamt.replace(/\s/g, '');

alert(str); die;

答案 3 :(得分:0)

你可以使用jQuery trim()删除左右空格,首先显示带输入值的空格然后点击删除空格然后删除空格

&#13;
&#13;
 function removeSpace() {
    var inputvalue = $('input[name="payment_act_invamt"]').val();

    inputvalue = inputvalue.trim();

    $('input[name="payment_act_invamt"]').val(inputvalue);
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
    <input type="text" name="payment_act_invamt" value="    9000">
    <input type="button" onclick="removeSpace()" value="Remove Space" />

</form>
&#13;
&#13;
&#13;

因此您可以使用此代码从输入字段中删除空格

&#13;
&#13;
  var inputvalue = $('input[name="payment_act_invamt"]').val();

        inputvalue = inputvalue.trim();

        $('input[name="payment_act_invamt"]').val(inputvalue);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
    <input type="text" name="payment_act_invamt" value="    9000">

</form>
&#13;
&#13;
&#13;