下面的腳本返回一個錯誤:獲取「event.target.id」作爲字符串
"Uncaught TypeError: Object Reg_8712 has no method 'indexof'
Reg_8712
是單選按鈕ID是誰開的事件。
腳本:
$("input:radio").change(function (event) {
alert(event.target.id); // this works! it returns it as string.
var eti = event.target.id; // 'eti' gets the object and not the string.
var n = eti.indexof("_"); // error! cannot indexof ('eti' is an object and not string)
var fid = eti.substring(n);
我怎樣才能獲得 'ETI' 作爲字符串?
方法名稱是'indexOf'(注意大寫字母O)。 –
邁克已經給出瞭解決方案,但如果你需要字符串強制轉換:'eti = String(event.target.id)'或者快速版本'eti = event.target.id +''' – TecHunter
謝謝Mike!它現在正在工作! – Eyal