如何仅使用此代码复制前两个字母?我真的不知道怎么办
function sync() {
var c = document.getElementById('c');
var a = document.getElementById('a');
a.value = c.value;
}
最佳答案
您可以使用字符串切片方法:
MyString.slice(0,2); //returns a new string with only the first two chars.
所以你可以写:
function sync() {
var c = document.getElementById('c');
var a = document.getElementById('a');
a.value = c.value.slice(0,2);// a now contains the first two chars from c
}
关于javascript - 从文本框中复制前两个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19394278/