此代码获取选择了多少个复选框(nchecked)。当选择了2个以上的盒子时它可以工作,但是当我没有选中复选框时没有输出警告。 应该有什么不同?
这里是获取复选框的代码
private int getCheckBoxes(){
int count = 0;
for (int i = 0; i < checkBoxes.size(); i++){
if (checkBoxes.get(i).isSelected()) {
count++;
}
}
return count;
}
private int getIndex(String cityName) {
return cities.indexOf(cityName);
}
//Here is the code that I'm putting out my warning
private void handleCitySelection (int index) {
if (checkBoxes.get(index).isSelected())
{
int nchecked = getCheckBoxes();
if (nchecked <= 0)
{
outputDistance.append("Please Select 2 Cities");
}
if(nchecked >= 3)
{
checkBoxes.get(index).setSelected(false);
}
else
if (nchecked == 1) {
city1 = index;
}
else
{
city2 = index;
}
}
}
每个复选框后面还有:
handleCitySelection(getIndex(evt.getActionCommand()));
提前致谢!!
答案 0 :(得分:1)
也许当你拨打这一行时
from django.shortcuts import redirect
from django.views.generic.edit import CreateView
class SongCreate(CreateView):
model = Song
fields=['song_name','audio']
def form_valid(self, form):
song = form.save(commit=False)
song.user = self.request.user
song.save()
return redirect(self.get_success_url())
如果选中索引处的框,则只执行以下if / else语句。如果没有选中,则不会执行这些语句,因此不会打印警告