javascript - django 选择字段值
问题描述
我在 Django 模型中创建了一个选择字段,但是当我尝试onchange
在 HTML 中调用时,ajax 代码不起作用。这是代码。当我单击此表单中的 match_recipient 时,我需要 ajax 上的值,但现在我没有得到该值。有显示undefined
价值
模型.py
type = (
('catchall' , 'Catch All'),
('match_recipient','Match Recipient'),
('match_header','Match Header'),
)
class Routes(models.Model):
expression_type = models.CharField(max_length=10,choices=type,default='catchall')
actions = models.CharField(max_length=100)
recipient = models.CharField(max_length=50)
header_name = models.CharField(max_length=50)
header_value = models.CharField(max_length=50)
priority = models.IntegerField()
user = models.ForeignKey(User, on_delete=models.CASCADE)
表格.py
type = (
('catchall' , 'Catch All'),
('match_recipient','Match Recipient'),
('match_header','Match Header'),
)
class RouteForm(forms.ModelForm):
expression_type = forms.ChoiceField(choices=type,widget=forms.Select(), required=True)
actions = forms.CharField(help_text='Required', widget=forms.TextInput(attrs={'placeholder': 'address@example.com'}))
recipient = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'recipient@example.com'}))
header_name = forms.CharField( widget=forms.TextInput(attrs={'placeholder': 'X-Header-Name'}))
header_value = forms.CharField( widget=forms.TextInput(attrs={'placeholder': 'Header Value'}))
priority = forms.IntegerField(initial=0)
class Meta:
model = Routes
fields = ('expression_type','actions','recipient','header_name','header_value','priority',)
模板.html
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#recipient").hide();
$("#header_name").hide();
$("#header_value").hide();
$("#expression_type").change(function() {
alert("ededde");
var a = $(this.text());
console.log(a);
alert(a);
if($(this).val() == "match_recipient"){
$("#recipient").show();
}else if($(this).val() == "match_header"){
$("#header_name").show();
$("#header_value").show();
}
});
});
</script>
</head>
<form action="{% url 'route' %}" method="POST">
{% csrf_token %}
<div class="field has-addons">
<div class="form-group row" id="expression_type">
<label class="col-sm-2 col-form-label">Expression Type</label>
<div class="col-sm-10">
{{ form.expression_type }}
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label">Action</label>
<div class="col-sm-10">
{{ form.actions }}
</div>
</div>
<div class="form-group row" id="recipient">
<label class="col-sm-2 col-form-label">Recipient</label>
<div class="col-sm-10">
{{ form.recipient }}
</div>
</div>
<div class="form-group row" id="header_name">
<label class="col-sm-2 col-form-label">Header Name</label>
<div class="col-sm-10">
{{ form.header_name }}
</div>
</div>
<div class="form-group row" id="header_value">
<label class="col-sm-2 col-form-label">Header Value</label>
<div class="col-sm-10">
{{ form.header_value }}
</div>
</div>
<div class="form-group row" id="priority">
<label class="col-sm-2 col-form-label">Priority</label>
<div class="col-sm-10">
{{ form.priority }}
</div>
</div>
<div class="control">
<button class="button is-info">
Add Route
</button>
</div>
</div>
</form>
解决方案
推荐阅读
- r - 如何制作显示单个基因与多个基因之间相关性的散点图?
- python - 在 scipy(python)中执行 B-Spline 拟合时如何更改基函数的数量?
- android-studio - Android Studio 3.2 beta 2 和 3.3 canary 2 每次都死机
- javascript - Javascript函数不适用于选择的插件
- php - 将某个 URL 目录下的 Apache 请求定向到一个文件,将所有其他请求定向到第二个文件
- android - Android - onclick 在模拟器 api 19 上不断崩溃,在其他地方工作正常
- c# - '列表
' 不包含 'ChoiceId ' 的定义并且没有扩展名...您是否缺少 using 指令或程序集引用?) - bash - How can I check if a variable that need to contain a number has been set?
- javascript - Phaser / TypeError:无法读取未定义的属性“then”
- python - 使用 gremlin-python 以列表作为属性值过滤边缘