我有一个html页面,我将选中的复选框传递给django视图作为参数完全正常。
的layout.html
<form action="{% url 'URL Which calls view' %}" method="post">
{% csrf_token %}
<label class="checkbox">
<input type="checkbox" name="checks" value="REG_AGREED_SUITE01">REG_AGREED_SUITE01
</label>
<hr>
<label class="checkbox">
<input type="checkbox" name="checks" value="REG_AGREED_SUITE02">REG_AGREED_SUITE02
</label>
<hr>
<label class="checkbox">
<input type="checkbox" name="checks" value="REG_AGREED_SUITE03">REG_AGREED_SUITE03
</label>
<hr>
form.py
class NameForm(forms.Form):
views.py
def view(request):
if request.method == 'POST':
form = NameForm(request.POST)
else:
form = NameForm()
print request.POST.getlist('checks')
form = NameForm(request.POST)
list1 = request.POST.getlist('checks')
TestSuite = ', '.join(list1)
现在我有一个情况,我希望在django视图中将单选按钮作为参数传递。我有一个json文件,使用java脚本我将拉取值并显示在html上。有没有办法,当我选择一个单选按钮时,我可以将它们作为django视图中的参数传递给上述情况?或者它是在Django中创建表单的最佳方法?
temp.json
[
{"STBStatus": "1", "RouterSNo": "R1", "STBLabel": "STB#1", "STBSno": "M11435TDS144"},
{"STBStatus": "1", "RouterSNo": "R1", "STBLabel": "STB#2", "STBSno": "M11543TH4292"},
{"STBStatus": "0", "RouterSNo": "R1", "STBLabel": "STB#3", "STBSno": "SN005"},
{"STBStatus": "1", "RouterSNo": "R1", "STBLabel": "STB#4", "STBSno": "M11509TD9937"},
{"STBStatus": "1", "RouterSNo": "R1", "STBLabel": "STB#5", "STBSno": "M11543TH4258"},
{"STBStatus": "0", "RouterSNo": "R1", "STBLabel": "STB#6", "STBSno": "SN005"},
{"STBStatus": "0", "RouterSNo": "R1", "STBLabel": "STB#7", "STBSno": "SN006"},
{"STBStatus": "0", "RouterSNo": "R1", "STBLabel": "STB#8", "STBSno": "SN007"}
]
使用Javascript:
<script>
function stbststus(){
show_alert()
$.getJSON("Json", function(result){
$("#STBStatus").empty();
$.each(result, function(i, item){
if(item.STBStatus == "1"){
colorclass = "available"
}
if(item.STBStatus == "0"){
colorclass = "offline"
}
if(item.STBStatus == "2"){
colorclass = "In use"
}
$("#STBStatus").append("<label class='radio'><input type='radio' name='optradio' class='optradio'>"+item.STBLabel +" <i class='fa fa-circle pull-right "+colorclass+"' aria-hidden='true'></i></label> <hr>");
});
});
}
</script>
的layout.html
<div class="quote-text scroller" id="STBStatus"> </div>
<div class="wrapper">
<div class="buttonHolder" onclick="pageload()">
<p><a href="Set_Top_Box" class="btn btn-primary btn-large">Get Settopbox Status »</a></p>
答案 0 :(得分:0)
Django不会处理onClick事件或任何排序,你最好使用javascript和jquery。
<强> Jquery的强>
$(function(){
$('input[type="radio"]').click(function(){
if ($(this).is(':checked'))
{
alert($(this).val());
}
});
});
或对于vanilla JavaScript,请检查此网址Get Value of Selected Radio Button。