批量修改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)