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的替换位置

posted @   风意不止  阅读(14218)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示