javascript – So ersetzen Sie den Inhalt im <p>-Tag in JS. Bitte geben Sie mir einen Rat.
为情所困
为情所困 2017-05-19 10:34:25
0
7
591
<p 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>
    </p>
    <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+"";
            
    }
    
  }

Ersetzen Sie Option 1, Option 2 und Option 3 durch den RadioData-Inhalt in den Daten. Bitte führen Sie mich an. Mein Code funktioniert nicht

为情所困
为情所困

Antworte allen(7)
淡淡烟草味

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
      }
      

左手右手慢动作
function init(){
    var $pArray = $("#rads input");
    $pArray.each(function(index,item){
       radioData.forEach(function(item1,index1){
           $(item).val(item1.name);
       });
    });
  }
小葫芦
<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>
漂亮男人

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

習慣沉默

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

我想大声告诉你
<!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>
    <p 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>
    </p>
    <button class="blueBtn" onclick="init()">替换</button>
</body>

</html>
小葫芦

你好,在你的提问中,是想替换掉 <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>
    <p 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>
    </p>
    <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>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage