我正在开发html调查页面,要求是从每行调查中只选择1个复选框,调查选项可能不同,请参阅下面的屏幕和html:
<div class="firstrow"><div class="srno"><p>1.</p></div><div class="survey"><span>I am satisfied with the quality, reliability and speed of the computer I use.</span></div></div>
<div class="options">
<span class="optpadding"><input type="checkbox" name="Strongly agree" value="Strongly agree"><span>Strongly agree</span></span>
<span class="optpadding"><input type="checkbox" name="Agree" value="Agree"><span>Agree</span></span>
<span class="optpadding"><input type="checkbox" name="Disagree" value="Disagree"><span>Disagree</span></span>
<span class="optpadding"><input type="checkbox" name="Strongly disagree" value="Strongly disagree"><span>Strongly disagree</span></span>
</div>
<div class="firstrow"><div class="srno"><p>2.</p></div><div class="survey"><span>The Software currently installed on my computer is adequate for my job function.</span></div></div>
<div class="options">
<span class="optpadding"><input type="checkbox" name="Strongly agree" value="Strongly agree"><span>Strongly agree</span></span>
<span class="optpadding"><input type="checkbox" name="Agree" value="Agree"><span>Agree</span></span>
<span class="optpadding"><input type="checkbox" name="Disagree" value="Disagree"><span>Disagree</span></span>
<span class="optpadding"><input type="checkbox" name="Strongly disagree" value="Strongly disagree"><span>Strongly disagree</span></span>
</div>
我如何使用jquery实现这一点,我已经尝试了很多,但只有一个调查问题就取得了成功。当我选择另一个问题复选框时,这将删除上一个问题选中复选框。
$(function () {
$('input:checkbox').click(function () {
if ($(this).is(':checked')) {
$('input:checkbox').not(this).prop('checked', false);
}
});
})
如何解决这个问题?
答案 0 :(得分:2)
要在多个选项中仅选择1个选项,而不是使用checkbox
使用radio
并为所有无线电提供相同的名称属于某个类别,以便只能在一个类别中检查一个无线电时间。
例如:
<p>Topic 1
<input type="radio" name="topic1">One
<input type="radio" name="topic1">Two
</p>
<p>Topic 2
<input type="radio" name="topic2">One
<input type="radio" name="topic2">Two
</p>
&#13;
答案 1 :(得分:2)
注意: - 实际上最好使用单选按钮,然后不需要jQuery。
但如果您想使用复选框,jQuery就像下面这样: -
$(function () {
$('input:checkbox').click(function () {
if ($(this).is(':checked')) {
$(this).parent().siblings().children("input:checkbox").prop('checked', false);
}
});
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="firstrow"><div class="srno"><p>1.</p></div><div class="survey"><span>I am satisfied with the quality, reliability and speed of the computer I use.</span></div></div>
<div class="options">
<span class="optpadding"><input type="checkbox" name="Strongly agree" value="Strongly agree"><span>Strongly agree</span></span>
<span class="optpadding"><input type="checkbox" name="Agree" value="Agree"><span>Agree</span></span>
<span class="optpadding"><input type="checkbox" name="Disagree" value="Disagree"><span>Disagree</span></span>
<span class="optpadding"><input type="checkbox" name="Strongly disagree" value="Strongly disagree"><span>Strongly disagree</span></span>
</div>
<div class="firstrow"><div class="srno"><p>2.</p></div><div class="survey"><span>The Software currently installed on my computer is adequate for my job function.</span></div></div>
<div class="options">
<span class="optpadding"><input type="checkbox" name="Strongly agree" value="Strongly agree"><span>Strongly agree</span></span>
<span class="optpadding"><input type="checkbox" name="Agree" value="Agree"><span>Agree</span></span>
<span class="optpadding"><input type="checkbox" name="Disagree" value="Disagree"><span>Disagree</span></span>
<span class="optpadding"><input type="checkbox" name="Strongly disagree" value="Strongly disagree"><span>Strongly disagree</span></span>
</div>
答案 2 :(得分:1)
使用<input type="radio" />
代替<input type="checkbox" />
但要修复您的jQuery使用.siblings()
而不是.not(this)
答案 3 :(得分:1)
尝试使用此代码 100%:
演示: https://output.jsbin.com/ketunanizo
https://jsbin.com/ketunanizo/edit?html,js,output
<强> HTML:强>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<div class="firstrow"><div class="srno"><p>1.</p></div><div class="survey"><span>I am satisfied with the quality, reliability and speed of the computer I use.</span></div></div>
<div class="options">
<span class="optpadding"><input type="checkbox" name="Strongly agree" value="Strongly agree"><span>Strongly agree</span></span>
<span class="optpadding"><input type="checkbox" name="Agree" value="Agree"><span>Agree</span></span>
<span class="optpadding"><input type="checkbox" name="Disagree" value="Disagree"><span>Disagree</span></span>
<span class="optpadding"><input type="checkbox" name="Strongly disagree" value="Strongly disagree"><span>Strongly disagree</span></span>
</div>
<div class="firstrow"><div class="srno"><p>2.</p></div><div class="survey"><span>The Software currently installed on my computer is adequate for my job function.</span></div></div>
<div class="options">
<span class="optpadding"><input type="checkbox" name="Strongly agree" value="Strongly agree"><span>Strongly agree</span></span>
<span class="optpadding"><input type="checkbox" name="Agree" value="Agree"><span>Agree</span></span>
<span class="optpadding"><input type="checkbox" name="Disagree" value="Disagree"><span>Disagree</span></span>
<span class="optpadding"><input type="checkbox" name="Strongly disagree" value="Strongly disagree"><span>Strongly disagree</span></span>
</div>
</body>
</html>
<强> JavaScript的:强>
<script>
$(function () {
$('input:checkbox').click(function () {
if ($(this).is(':checked')) {
$(this).closest('.options').find('input:checkbox').not(this).prop('checked', false);
}
});
})
</script>
答案 4 :(得分:0)
试试这个,它肯定会有用......
$('input:checkbox').click(function () {
if ($(this).is(':checked')) {
$(this).parent().siblings().children().prop('checked', false);
}
});