JS中的替换,以及替换指定位置的字符串

批量修改name属性中的值

 // 渲染完成,开始修改ansewer的name属性
                             $('.sub_timu_zong_tihao').each(function(i){
                                $(this).find('input[name*=bianhao]').each(function(){
                                    // 首先获取name的值,对
                                    console.log('正在修改bianhao')
                                    var old=$(this).attr('name')
                                    var now=old.replace('bianhao',i)
                                    $(this).attr('name',now)
                                })

                            })

修改题目的编号

 function shuaxin_tihao(){
                                    // 渲染完成,开始修改name中的tihao
                                $('.sub_timu_zong_tihao').each(function(i){
                                    $(this).find('input[name*=tihao]').each(function(){
                                        // 首先获取name的值,对
                                        console.log('正在修改input中的tihao')
                                        var old=$(this).attr('name')
                                        var now=old.replace('tihao',i)
                                        $(this).attr('name',now)
                                    })

                                    $(this).find('textarea[name*=tihao]').each(function(){
                                        // 首先获取name的值,对
                                        console.log('正在修改textarea中的tihao')
                                        var old=$(this).attr('name')
                                        var now=old.replace('tihao',i)
                                        $(this).attr('name',now)
                                    })

                                    $(this).find('*[id*=tihao]').each(function(){
                                        // 首先获取name的值,对
                                        console.log('正在修改id中的tihao')
                                        var old=$(this).attr('id')
                                        var now=old.replace('tihao',i)
                                        $(this).attr('id',now)
                                    })

                                })
                        }

由于第一次是根据名字进行替换,这次进行替换,已经没有名字了。所以,这时候,根据位置去替换

根据位置替换字符

//str:原始字符串,index,开始位置,changeStr,改变后的字
function changeStr(str,index,changeStr){
	 return str.substr(0, index) + changeStr+ str.substr(index + changeStr.length);
	 }
//记住的,传值的时候,第三个要传字符串
   var str="row[answer5][tihao]"
     var a=changeStr(str,13,'1')
     console.log(a)

在php生成页面的时候,就确定每个tihao的替换位置

猜你喜欢

转载自www.cnblogs.com/cn-oldboy/p/13198161.html
今日推荐