【问题标题】:passing radio buttons from javascript to django views as a paramater将单选按钮从 javascript 传递到 django 视图作为参数
【发布时间】:2017-02-15 13:27:25
【问题描述】:

我有一个 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 &raquo;</a></p> 

【问题讨论】:

    标签: php python html django


    【解决方案1】:

    Django 不会处理 onClick 事件或任何类似的事件,您最好使用 javascript 和/或 jquery。

    jQuery

    $(function(){
      $('input[type="radio"]').click(function(){
        if ($(this).is(':checked'))
        {
          alert($(this).val());
        }
      });
    });
    

    或者对于普通 JavaScript,请检查此网址 Get Value of Selected Radio Button

    【讨论】:

      猜你喜欢
      • 2019-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-21
      • 2020-06-09
      • 1970-01-01
      • 2021-04-14
      • 1970-01-01
      相关资源
      最近更新 更多