此代码允许用户仅键入某些字符:
var companies = [
{
id: 1,
name: 'Google',
link: 'http://google.com/'
},
{
id: 2,
name: 'Microsoft',
link: 'http://microsoft.com/'
},
{
id: 3,
name: 'Apple',
link: 'http://apple.com'
}
];
var tbl = document.createElement('table');
var input = document.getElementById('input');
var thead = document.createElement("thead");
var tbody = document.createElement("tbody");
var tr_head = document.createElement("tr");
var th_id = document.createElement("th");
var th_name = document.createElement("th");
var th_link = document.createElement("th");
th_id.textContent = "Id";
th_name.textContent = "Name";
th_link.textContent = "link";
tr_head.appendChild(th_id);
tr_head.appendChild(th_name);
tr_head.appendChild(th_link);
thead.appendChild(tr_head);
for(var i = 0; i < companies.length; i++) {
var tr_body = document.createElement("tr");
var td_id = document.createElement("td");
var td_name = document.createElement("td");
var td_link = document.createElement("td");
// var id = companies[i].id;
// var name = companies[i].name;
// var link = companies[i].link;
td_id.textContent = companies[i].id;
td_name.textContent = companies[i].name;
td_link.textContent = companies[i].link;
tr_body.appendChild(td_id);
tr_body.appendChild(td_name);
tr_body.appendChild(td_link);
tbody.appendChild(tr_body);
}
tbl.appendChild(thead);
tbl.appendChild(tbody);
console.log(tbl);
// input.appendChild(tbl);
我怎样才能使它只允许一个名为&#34; txtField1&#34;的文本字段?
答案 0 :(得分:2)
您可以检查textField是否是shouldChangeCharactersInRange
方法中当前选择的那个,因为您可以在此方法的参数中看到,您可以访问与当前textField对应的属性textField
您正在选择,所以请在开头查看:
if texField == txtField1 {
// Do your stuff
} else {
// Do something else
}
答案 1 :(得分:1)
为文本字段中对其唯一的标记指定值(例如,1)。
txtField1.tag = 1
然后,像这样更新你的方法:
func textField(
textField: UITextField,
shouldChangeCharactersInRange range: NSRange,
replacementString string: String)
-> Bool
{
if textField.tag == 1 {
let set = NSCharacterSet(charactersInString: allowedChars)
let filtered = string
.componentsSeparatedByCharactersInSet(set)
.joinWithSeparator("")
return filtered != string
}
return true
}
你也可以省略;在斯威夫特。