我知道我们可以设置一个像.val('');
这样的文本框值,但在我的情况下,我需要一些如何通过选择和删除来删除/删除输入中的现有值。现在我可以选择值,但是如何删除或删除它?
$("#open-cal").on("click", function() {
var $inbox = $('#whereItsAt');
$inbox.select();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="btn btn-default" id="open-cal" href="#" role="button">Select And Remove</button>
<br />
<br />
<input id="whereItsAt" type="text" value="$2,000,000" />
答案 0 :(得分:2)
您可以获取selectionStart索引,selectionEnd,然后将其替换为您喜欢的内容。
在此示例中,我获取所选范围,然后将其替换为空字符串,然后将其应用于输入值。
$("#open-cal").on("click", function() {
var $inbox = $('#whereItsAt');
$inbox.select();
var selectionStart = $inbox[0].selectionStart;
var selectionEnd = $inbox[0].selectionEnd;
var inputVal = $inbox.val();
var str = inputVal.substring(selectionStart, selectionEnd)
$inbox.val(inputVal.replace(str, ''))
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="btn btn-default" id="open-cal" href="#" role="button">Select And Remove</button>
<br />
<br />
<input id="whereItsAt" type="text" value="$2,000,000" />
&#13;
答案 1 :(得分:0)
如果通过“选择”一个值意味着获取值然后使用相同的方法而不发送任何内容它将返回值: $( '#whereItsAt')VAL();