我不希望当用户点击提交按钮以在文本框中获取值时。
base.html文件
var Value = document.getElementById('vat1').value;
alert(Value);
脚本
def call_operator(request):
text_box_value = request.POST.get('vat')
print(text_box_value)
print("get operator name and circle name")
template_name = 'mobile_recharge.html'
extended_template = 'base.html'
if request.user.is_authenticated():
extended_template = 'base_login.html'
return render(
request, template_name,
{'extended_template': extended_template}
)
views.py
url(r'^call_operator/$', call_operator)
urls.py
<script type="text/javascript">
$(document).ready(function(){
$('#vat1').keyup(function(){
if ($(this).val().length == 10){
$('your_form').submit();
alert("condition satisfied");
$.ajax({
type: "GET",
url: "http://localhost:8090/call_operator/"
});
}
});
});
</script>
脚本
<form method="POST" role="form" name="your_form">
<input type="text" name="vat" id="vat1" placeholder="Please Enter 10 digits number">
</form>
html文件
{{1}}
答案 0 :(得分:1)
您可以使用Ajax。这是另一种方法
base.html文件
<form method="POST" name="your_form" action="{% url 'your_app:your_url' %}">
<input type="text" name="vat" id="vat1" placeholder="Please Enter 10 digits number">
</form>
<script type="text/javascript">
$(document).ready(function(){
$('#vat1').keyup(function(event){
if (event.which == 13 || event.keyCode == 13){
if ($(this).val().length == 10){
$('your_form').submit();
}else{
// Show message if the text length is lesser than 10
}
}
});
});
</script>