function count() {
$("input:radio")[1].html = alert("We're from the same site");
$("input:radio")[0][2][3].html = alert("We are not from the same site");
}

<html>
<head>
<title>jQuery Script</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script type="text/javascript" language="javascript">
</script>
</head>
<body>
<h1>Select one campus</h1>
<form>
<input id="Belfast" type="radio"> Belfast
<input id="Jordanstown" type="radio"> Jordanstown
<input id="Magee" type="radio"> Magee
<input id="Coleraine" type="radio"> Coleraine
<br> <br>
<input type = "button" value="Count" onclick="count()">
</form>
<input type="button" value="Reload page" onclick="reloadPage()">
</body>
</html>
&#13;
有人可以帮我解决这段代码吗?我现在正在学习jQuery,我正在尝试创建一个jQuery脚本来检查访问者是否与我在同一所大学校园。我使用以下代码:
当我在屏幕上选择第二个选项时,它按预期工作,但对于任何其他选项,它似乎给了我完全相同的警告框,如下所示:
Selecting the correct campus site
Selecting any other listed campus site
我认为错误位于我的count()函数中,但我看不出问题所在:
function count() {
$("input:radio")[1].html = alert("We're from the same site");
$("input:radio")[0][2][3].html = alert("We are not from the same site");
}
答案 0 :(得分:2)
<html>
<head>
<title>jQuery Script</title>
</head>
<body>
<h1>Select one campus</h1>
<form>
<input id="Belfast" value="Belfast" name="campus" type="radio"> Belfast
<input id="Jordanstown" value="Jordanstown" name="campus" type="radio"> Jordanstown
<input id="Magee" value="Magee" name="campus" type="radio"> Magee
<input id="Coleraine" value="Coleraine" name="campus" type="radio"> Coleraine
<br> <br>
<input type = "button" value="Count" id="count">
</form>
<input type="button" value="Reload page" id="reload">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" language="javascript">
$('#count').click(function() {
var campus = $("input[name='campus']:checked");
if(campus.length > 0){
if(campus.val()=="Belfast") alert('same campus');
else alert('different campus')
}
});
$('#reload').click(function() {
location.reload();
});
</script>
</body>
</html>
答案 1 :(得分:1)
我认为您对预期内容的高级描述是&#34;如果选择单选按钮#1,则提醒同一网站&#39;消息,如果选择单选按钮#0,#2或#3,则显示“不同的站点”#39; 。消息&#34;
您的代码有几个错误,但我会尝试解决最基本的误解:
链式括号表示法对先前访问的结果进行属性访问。 foo[0][2][3]
说{&#34;从0
访问媒体资源foo
,然后从之前访问的结果(2
{{1}访问媒体资源0
然后访问上一次访问中的属性foo
(来自3
2
0
上foo
的结果。它相当于
result1 = foo[0];
result2 = result1[2];
result3 = result2[3];
我认为,这并不是您想要的远程 - 您似乎希望您从中选择0
,2
和3
最左边的表达。
您要在此处查看是否选择了特定的单选按钮。 .html
肯定不是这样做的方式,这不是DOM中具有任何意义的属性。您可以通过selected
检查DOM $("input:radio")[1].selected
属性。
你想要的是一个条件:&#34; 如果按钮#1被选中,那么......&#34;您可以使用if
构造执行此操作:
if($("input:radio")[1].selected) {
alert("same site");
}
然后,如果其他条件成立,您可以使用else
来运行代码:
if($("input:radio")[1].selected) {
alert("same site");
} else {
alert("different site");
}
jQuery-idomatic方法越多(如果稍后更改选项的顺序就越不脆弱)就是编写一个jQuery选择器,使用:checked
selector查找当前选项,然后比较{{1该元素:
id
答案 2 :(得分:0)
function count() {
var selectedCampus = $("input[name=campus]:checked").val();
if(typeof selectedCampus === "undefined") {
alert("Select a campus");
return;
}
var myCampus = "jordanstown";
if(selectedCampus === myCampus){
alert("We're from the same site");
} else {
alert("We are not from the same site");
}
}
&#13;
<html>
<head>
<title>jQuery Script</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script type="text/javascript" language="javascript">
</script>
</head>
<body>
<h1>Select one campus</h1>
<form>
<input id="Belfast" name="campus" value="belfast" type="radio"> Belfast
<input id="Jordanstown" name="campus" value="jordanstown" type="radio"> Jordanstown
<input id="Magee" name="campus" value="magee" type="radio"> Magee
<input id="Coleraine" name="campus" value="coleraine" type="radio"> Coleraine
<br> <br>
<input type = "button" value="Count" onclick="count()">
</form>
<input type="button" value="Reload page" onclick="reloadPage()">
</body>
</html>
&#13;
答案 3 :(得分:0)
您需要添加名称属性,以便一次只选择其中一个,例如单选按钮组。检查下面的代码
function count(){
$("input[type=radio]:checked").each(function(){
if($(this).prop('id') === "Belfast"){
alert("We're from the same site");
}
else{
alert("We are not from the same site");
}
});
}
function reloadPage(){
location.reload();
}
&#13;
<html>
<head>
<title>jQuery Script</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>
<h1>Select one campus</h1>
<form>
<input id="Belfast" name="uni" type="radio"> Belfast
<input id="Jordanstown" name="uni" type="radio"> Jordanstown
<input id="Magee" name="uni" type="radio"> Magee
<input id="Coleraine" name="uni" type="radio"> Coleraine
<br> <br>
<input type = "button" value="Count" onclick="count()">
</form>
<input type="button" value="Reload page" onclick="reloadPage()">
</body>
</html>
&#13;
答案 4 :(得分:0)
试试这个..
<强> JS 强>
IndexArea
<强> HTML 强>
// on click of input with value of Count
$("input[value='Count']").click(function() {
// if school is yours
if ($('#Jordanstown').prop('checked')) {
alert("We're from the same site");
// else, all other schools are not yours
} else {
alert('We are not from the same site');
}
})
// i didnt play with this
function reloadPage() {
location.reload();
}
<强>拨弄强>