0
<div id="rads" class="formStyle selectOrder">
        <p>
             <input type="radio"  name="question" value="oui" checked >选项1
        </p>
        <p>
            <input type="radio"  name="question" value="non" > 选项2
        </p>
        <p>
            <input type="radio"  name="question" value="non" > 选项3
        </p>
    </div>
    <button class="blueBtn" onclick = "init()">替换</button>
var radioData = [{name:'紧急', value:1}, {name:'重要', value:2}, {name:'普通', value:3}];
function init(){
    for(var  i=0;i<radioData.length;i++){
            var pArray = $("#rads p");
            var radioObj = radioData[i];
            pArray[i].value =radioObj.name+"";
            
    }
    
  }

将选项1 选项2 选项3 替换成 数据中的radioData 内容 求大神指导 我的代码不起作用

7个回答

0

已采纳

var oBox = document.getElementsByTagName("p");

    var radioData = [{name:'紧急', value:1}, {name:'重要', value:2}, {name:'普通', value:3}];
      for(var i=0;i<radioData.length;i++){
          console.log(oBox)
          oBox[i].childNodes[2].nodeValue=radioData[i].name
      }
      

0
function init(){
    var $pArray = $("#rads input");
    $pArray.each(function(index,item){
       radioData.forEach(function(item1,index1){
           $(item).val(item1.name);
       });
    });
  }
0
<script src="jquery-1.11.2.min.js" ></script>
<script>

var radioData = [{name:'紧急', value:1}, {name:'重要', value:2}, {name:'普通', value:3}];

function init(){
    for (x in radioData) {
        $("#rads p").eq(x).text( radioData[x].name )
    }
}

</script>
0

你要替换的是input的value不是p的value,获取到每个input

0

你的代码是没有大问题的,不需要大改动
pArray[i].innerHTML =radioObj.name+""; 把value改成innerHTML就可以了

0
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="js/jquery-1.11.3.min.js"></script>
    <script>
        var radioData = [{
            name: '紧急',
            value: 1
        }, {
            name: '重要',
            value: 2
        }, {
            name: '普通',
            value: 3
        }];

        function init() {
            var pArray = $("p input");
            for (var i = 0; i < radioData.length; i++) {
                var radioObj = radioData[i];
                $(pArray[i]).next().text(radioObj.name);
            }
        }
    </script>
</head>

<body>
    <div id="rads" class="formStyle selectOrder">
        <p>
            <input type="radio" name="question" value="oui" checked><label>选项1<label></input>
        </p>
        <p>
            <input type="radio" name="question" value="non"><label>选项2<label>
        </p>
        <p>
            <input type="radio" name="question" value="non"><label>选项3<label>
        </p>
    </div>
    <button class="blueBtn" onclick="init()">替换</button>
</body>

</html>
0

你好,在你的提问中,是想替换掉 <input> 标签后面的文本,而不是标签的 value 值。
所以,你使用 pArray[i].value =radioObj.name+""; 是不正确的。
下面是我给出的代码,仅供参考:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Demo</title>
    <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
</head>

<body>
    <div id="rads" class="formStyle selectOrder">
        <p>
            <input type="radio" name="question" value="oui" checked> 选项1
        </p>
        <p>
            <input type="radio" name="question" value="non"> 选项2
        </p>
        <p>
            <input type="radio" name="question" value="non"> 选项3
        </p>
    </div>
    <button class="blueBtn" onclick="init()">替换</button>
</body>
<script>
var radioData = [{
    name: '紧急',
    value: 1
}, {
    name: '重要',
    value: 2
}, {
    name: '普通',
    value: 3
}];

function init() {
    for (var i = 0; i < radioData.length; i++) {
        var pArray = $("#rads p");
        var radioObj = radioData[i];
        pArray[i].innerHTML = /\<.*\>/.exec(pArray[i].innerHTML)[0] + '&nbsp;' + radioObj.name;
    }
}
</script>

</html>

撰写答案