admin管理员组文章数量:1296858
var id = $('.getval_'+product_id+''+index+'').attr("id");
var value = "";
console.log(id);
//data that return by id
6059
s2id_6061
6122
s2id_8410
if (id.indexOf('s2id_') > 0) {
value = $('.getval_'+product_id+''+index+'').select2('val');
}else{
value = $('.getval_'+product_id+''+index+'').val();
}
Question: The code above is show that when the ID return by jquery, then it will show a list of data, so what I want is when the ID return, if the in front of ID don't have s2id_ it will go else statement. But the code return an error which is (Uncaught TypeError: Cannot read property 'indexOf' of undefined)
var id = $('.getval_'+product_id+''+index+'').attr("id");
var value = "";
console.log(id);
//data that return by id
6059
s2id_6061
6122
s2id_8410
if (id.indexOf('s2id_') > 0) {
value = $('.getval_'+product_id+''+index+'').select2('val');
}else{
value = $('.getval_'+product_id+''+index+'').val();
}
Question: The code above is show that when the ID return by jquery, then it will show a list of data, so what I want is when the ID return, if the in front of ID don't have s2id_ it will go else statement. But the code return an error which is (Uncaught TypeError: Cannot read property 'indexOf' of undefined)
Share edited Jun 9, 2017 at 4:51 ASR 1,8115 gold badges25 silver badges33 bronze badges asked Jun 9, 2017 at 3:01 Ch HongCh Hong 3781 gold badge6 silver badges20 bronze badges 1-
1
+''
? This is not needed at any point in your code. – nnnnnn Commented Jun 9, 2017 at 3:04
1 Answer
Reset to default 4You need to check whether id
itself is set, and there are two ways of going about this.
Either chain the if (id.indexOf('s2id_') > 0)
condition inside an outer if
condition that checks for id
:
if (id) {
if (id.indexOf('s2id_') > 0) {
value = $('.getval_'+product_id+''+index+'').select2('val');
}else{
value = $('.getval_'+product_id+''+index+'').val();
}
}
Or check for both conditions with an and statement:
if (id && id.indexOf('s2id_') > 0) {
value = $('.getval_'+product_id+''+index+'').select2('val');
}else{
value = $('.getval_'+product_id+''+index+'').val();
}
Hope this helps! :)
本文标签: jqueryUncaught TypeError Cannot read property 39indexOf39 of undefined in javascriptStack Overflow
版权声明:本文标题:jquery - Uncaught TypeError: Cannot read property 'indexOf' of undefined in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741645326a2390153.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论