本文介绍了获取"event.target.id"作为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下脚本返回错误:

"Uncaught TypeError: Object Reg_8712 has no method 'indexof' 

Reg_8712是触发事件的单选按钮ID.

Reg_8712 is the radio button id who fired the event.

脚本:

$("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'作为字符串?

How can I get the 'eti' as string?

推荐答案

如果某些内容确实不是字符串,则最简单的转换方法是使用通用的.toString()方法:

In case something is indeed not a string, most simple way to convert is use the generic .toString() method:

var eti =  event.target.id.toString();
var n = eti.indexOf("_");

简单的测试用例证明了这一点.

这篇关于获取"event.target.id"作为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 16:35