cari

Rumah  >  Soal Jawab  >  teks badan

javascript - Bagaimana untuk menggantikan kandungan dalam tag <p> dalam JS Sila beri saya nasihat.

<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+"";
            
    }
    
  }

Ganti pilihan 1, pilihan 2, dan pilihan 3 dengan kandungan radioData dalam data Sila bimbing saya Kod saya tidak berfungsi

为情所困为情所困2738 hari yang lalu546

membalas semua(7)saya akan balas

  • 淡淡烟草味

    淡淡烟草味2017-05-19 10:36:25

    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
          }
          

    balas
    0
  • ringa_lee

    ringa_lee2017-05-19 10:36:25

    function init(){
        var $pArray = $("#rads input");
        $pArray.each(function(index,item){
           radioData.forEach(function(item1,index1){
               $(item).val(item1.name);
           });
        });
      }

    balas
    0
  • 高洛峰

    高洛峰2017-05-19 10:36:25

    <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>

    balas
    0
  • 漂亮男人

    漂亮男人2017-05-19 10:36:25

    Apa yang anda mahu gantikan ialah nilai input, bukan nilai p, dan dapatkan setiap input

    balas
    0
  • 習慣沉默

    習慣沉默2017-05-19 10:36:25

    Tiada masalah besar dengan kod anda, tiada perubahan besar diperlukan
    pArray[i].innerHTML =radioObj.name+""; Cuma tukar nilai kepada innerHTML

    balas
    0
  • 我想大声告诉你

    我想大声告诉你2017-05-19 10:36:25

    <!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>

    balas
    0
  • 高洛峰

    高洛峰2017-05-19 10:36:25

    Hello, dalam soalan anda, anda ingin menggantikan <input> 标签后面的文本,而不是标签的 value 值。
    所以,你使用 pArray[i].value =radioObj.name+""; yang tidak betul.
    Berikut adalah kod yang saya berikan untuk rujukan sahaja:

    <!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>
    

    balas
    0
  • Batalbalas