<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+"";
}
}
Replace option 1, option 2, and option 3 with the radioData content in the data. Please guide me. My code does not work.
淡淡烟草味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
}
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);
});
});
}
高洛峰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>
漂亮男人2017-05-19 10:36:25
What you want to replace is the value of input, not the value of p, and get each input
習慣沉默2017-05-19 10:36:25
There is no big problem with your code, no major changes are needed
pArray[i].innerHTML =radioObj.name+""; Just change the value to innerHTML
我想大声告诉你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>
高洛峰2017-05-19 10:36:25
Hello, in your question, you want to replace <input>
标签后面的文本,而不是标签的 value
值。
所以,你使用 pArray[i].value =radioObj.name+"";
which is incorrect.
The following is the code I gave for reference only:
<!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] + ' ' + radioObj.name;
}
}
</script>
</html>