gpt4 book ai didi

javascript - 基于单选按钮,如何根据单选值列出html文档中的星星?

转载 作者:行者123 更新时间:2023-12-02 23:56:05 26 4
gpt4 key购买 nike

我正在尝试执行以下操作:每次您选择单选按钮时,这些按钮都必须变成星星。每个数字必须具有相同的星星数。

我只能使第一行起作用;如图所示。

我没有使用jquery。只有普通的 javascript(纯 javascript)

const radio = document.getElementsByName("gender");
const boton = document.getElementById("btnBoton");
const listado = document.getElementById('contenedor-filas');

boton.addEventListener('click', enviaNumeros);

function enviaNumeros()
{
radio.forEach(function(elementos){
if(elementos.checked)
{
estrellas = document.createElement('h3');
estrellas.textContent = elementos.value;
listado.appendChild(estrellas);
console.log(elementos.value)
}
});

ponerEstrellas();
}


function ponerEstrellas()
{
let c = document.getElementsByTagName('h3');

for (i = 0; i < c.length; i++){
if (c[i].textContent === '5'){
document.querySelector('h3').innerText = "*****";
}
}
}
<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<title>Review System</title>
<!-- style sheet -->
<!-- Font Awesome Icon Library -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>


<body>

<div class="container-fluid">
<div class="row">
<input type="radio" name="gender" value="5" id="radio5"> 5<br>
<input type="radio" name="gender" value="4" id="radio4"> 4<br>
<input type="radio" name="gender" value="3" id="radio3"> 3<br>
<input type="radio" name="gender" value="2" id="radio2"> 2<br>
<input type="radio" name="gender" value="1" id="radio1"> 1<br>
<input type="submit" value="enviar" id="btnBoton">
</div>
</div>

<section class="lista-comentarios">
<div class="container-fluid" id="contenedor-filas">
<h1>Comment List</h1>
</div>
</section>



<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"
integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1"
crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
crossorigin="anonymous"></script>
<script src="pruebas.js"></script>
</body>

</html>

感谢你们的帮助。

最佳答案

您似乎要添加带有单选按钮数值作为文本的行,然后将每一行从数字转换为“*”。为什么不在添加行之前转换为“*”?那么你只需要一个函数。

const radio = document.getElementsByName("gender");
const boton = document.getElementById("btnBoton");
const listado = document.getElementById('contenedor-filas');

boton.addEventListener('click', enviaNumeros);

function enviaNumeros()
{
radio.forEach(function(elementos){
if(elementos.checked)
{
estrellas = document.createElement('h3');
let s = '';
for (let i = 0; i < elementos.value; i++) {
s += '*';
}

estrellas.textContent = s;
listado.appendChild(estrellas);
}
});
}
<div class="container-fluid">
<div class="row">
<input type="radio" name="gender" value="5" id="radio5"> 5<br>
<input type="radio" name="gender" value="4" id="radio4"> 4<br>
<input type="radio" name="gender" value="3" id="radio3"> 3<br>
<input type="radio" name="gender" value="2" id="radio2"> 2<br>
<input type="radio" name="gender" value="1" id="radio1"> 1<br>
<input type="submit" value="enviar" id="btnBoton">
</div>
</div>

<section class="lista-comentarios">
<div class="container-fluid" id="contenedor-filas">
<h1>Comment List</h1>
</div>
</section>

关于javascript - 基于单选按钮,如何根据单选值列出html文档中的星星?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55386927/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com