我不知道为什么这段代码不起作用。我需要一个简单的解决方案。这只是一个大学项目,而不必执行以下操作: 我想做一个ti,以便当有人输入他们的姓名,位置和住址时,以这种形式弹出性别和评论警报框:
Name, Location, eAddress, Gender
comment...
代码:
<div style="font-family:'Cinzel';text-align:center;">
Ime: <input type="text" name="Ime" value=""><br></br>
Lokacija: <input type="text" name="Lokacija" value=""></br><br>
E Adresa: <input type="text" name="eAdresa" value=""></br><br>
<form>
<input type="radio" name="gender" value="male" checked> Muško
<input type="radio" name="gender" value="female"> Žensko
<input type="radio" name="gender" value="other"> Drugo<br>
<button onclick="Ispis()">Pošalji</button>
</form>
<textarea rows="4" cols="50" name="Komentar" form="usrform" placeholder="Ostavite komentar..."></textarea>
</div>
<script>
function Ispis(){
var Ime=getElementsByName=("Ime")[0].value;
var lokacija=getElementsByName=("Lokacija")[0].value;
var eAdresa=getElementsByName=("eAdresa")[0].value;
var Pol=getElementsByName=("gender")[0].value;
alert(Ime+","+Lokacija+","+eAdresa+","+Gender+"<br>"+Komentar);
}
</script>
答案 0 :(得分:5)
您的代码存在一些问题:
lokacija=document.getElementsByName("Lokacija")[0].value
需要文档。并且在getElementsByName之后不需要= :checked
queryselector:var Gender = document.querySelector('[name="gender"]:checked').value
function Ispis(){
var Ime = document.getElementsByName("Ime")[0].value;
var lokacija = document.getElementsByName("Lokacija")[0].value;
var Gender = document.getElementsByName("gender")[0].value;
var Gender = document.querySelector('[name="gender"]:checked').value;
var eAdresa = document.getElementsByName("eAdresa")[0].value;
var Komentar = document.getElementsByName("Komentar")[0].value;
alert(Ime+","+lokacija+","+eAdresa+","+Gender+"\n"+Komentar);
}
<div style="font-family:'Cinzel';text-align:center;">
Ime: <input type="text" name="Ime" value=""><br><br>
Lokacija: <input type="text" name="Lokacija" value=""><br><br>
E Adresa: <input type="text" name="eAdresa" value="">
<form>
<input type="radio" name="gender" value="male" checked> Muško
<input type="radio" name="gender" value="female"> Žensko
<input type="radio" name="gender" value="other"> Drugo<br>
<input type="button" onclick="Ispis()" value="alert!">
</form>
<textarea rows="4" cols="50" name="Komentar" form="usrform" placeholder="Ostavite komentar..."></textarea>
</div>
答案 1 :(得分:1)
我认为这是因为提交您的表单的按钮需要尝试
<input type="button" onclick="Ispis()" value="Yourtext">
然后使用小写字母重命名变量,区分大小写
答案 2 :(得分:0)
您有一些语法错误,这里是有效版本:
function Ispis() {
var ime = document.getElementsByName("ime")[0].value;
var lokacija = document.getElementsByName("lokacija")[0].value;
var eAdresa = document.getElementsByName("eadresa")[0].value;
var pol = document.getElementsByName("gender")[0].value;
var komentar = document.getElementsByName("komentar")[0].value;
alert(ime + "," + lokacija + "," + eAdresa + "," + pol + "<br>" + komentar);
}
<div style="font-family:'Cinzel';text-align:center;">
<form>
Ime: <input type="text" name="ime" value=""><br> Lokacija: <input type="text" name="lokacija" value=""><br> E Adresa: <input type="text" name="eadresa" value=""><br>
<input type="radio" name="gender" value="male" checked> Muško
<input type="radio" name="gender" value="female"> Žensko
<input type="radio" name="gender" value="other"> Drugo<br>
<button type="button" onclick="Ispis()">Pošalji</button>
</form>
<textarea rows="4" cols="50" name="komentar" form="usrform" placeholder="Ostavite komentar..."></textarea>
</div>
使用ES6编写:
(function () {
const getValueFromElement = name => document.getElementsByName(name)[0].value
const ispis = () => {
const ime = getValueFromElement("ime");
const lokacija = getValueFromElement("lokacija");
const eAdresa = getValueFromElement("eadresa");
const pol = getValueFromElement("gender");
const komentar = getValueFromElement("komentar");
alert(`${ime},${lokacija},${eAdresa},${pol},<br>${komentar}`)
}
const button = document.getElementById('button')
button.addEventListener('click', () => ispis())
}());
<div style="font-family:'Cinzel';text-align:center;">
<form>
Ime: <input type="text" name="ime" value=""><br> Lokacija: <input type="text" name="lokacija" value=""><br> E Adresa: <input type="text" name="eadresa" value=""><br>
<input type="radio" name="gender" value="male" checked> Muško
<input type="radio" name="gender" value="female"> Žensko
<input type="radio" name="gender" value="other"> Drugo<br>
<button id="button" type="button">Pošalji</button>
</form>
<textarea rows="4" cols="50" name="komentar" form="usrform" placeholder="Ostavite komentar..."></textarea>
</div>
答案 3 :(得分:0)
您可以按以下方式更改代码以起作用...
<div style="font-family:'Cinzel';text-align:center;">
Ime: <input type="text" name="Ime" value=""><br></br>
Lokacija: <input type="text" name="Lokacija" value=""></br><br>
E Adresa: <input type="text" name="eAdresa" value=""></br><br>
<form>
<input type="radio" name="gender" value="male" checked> Muško
<input type="radio" name="gender" value="female"> Žensko
<input type="radio" name="gender" value="other"> Drugo<br>
<button onclick="Ispis()">Pošalji</button>
</form>
<textarea rows="4" cols="50" name="Komentar" form="usrform" placeholder="Ostavite komentar..."></textarea>
</div>
<script>
function Ispis(){
var Ime=document.getElementsByName("Ime")[0].value;
var lokacija=document.getElementsByName("Lokacija")[0].value;
var eAdresa=document.getElementsByName("eAdresa")[0].value;
var Gender=document.getElementsByName("gender")[0].value;
var Komentar=document.getElementsByName("Komentar")[0].value;
debugger
alert(Ime+","+lokacija+","+eAdresa+","+Gender+"\n"+Komentar);
}
</script>