简体   繁体   English

如何在Django Form中使用Jquery脚本的值?

[英]How to use the value of Jquery script in Django Form?

I have a follwoing Jquery code, which create a Time Picker : 我有一个下面的Jquery代码,它创建一个Time Picker

<input class="time" type="text" name="start" id="start" />
$('input.time').timepicker();

And i want to use this time picker value which comes into the text in Django-Forms. 我想使用这个时间选择器值,它出现在Django-Forms中的文本中。

My models.py : 我的models.py

class UsertTime(models.Model):
user = models.ForeignKey(User, blank=False)
data = models.CharField(max_length=30)
timestamp = models.DateTimeField(blank=True, null=True)

my forms.py : 我的forms.py

class UserTimeForm(forms.ModelForm):
    class Meta:
    model = UserTime

In my template.html : 在我的template.html

<form action="" method="POST">
{{ form.as_p }}
<input type="submit" value="{% trans "Add " %}" /> <a href="{% url host_list %}">{% trans "Cancel" %}</a>
</form>

<input class="time" type="text" name="start" id="start" />
$('input.time').timepicker();

Now how should i fill the timestamp Field by the Jquery written input. 现在,我应该如何通过Jquery书面输入填充timestamp字段。 Following is the refer link for the Jquery Code: 以下是Jquery代码的引用链接:

Link for the Jquery sample jQuery示例的链接

I might also be misunderstanding; 我也可能会误会; but, can you not just inspect the output of form.as_p and operate on that input instead of $('input.time') 但是,您不能只检查form.as_p的输出并对该输入进行操作,而不是$('input.time')

$('#id_timestamp').timepicker()

Does this not work: 这不起作用:

<input class="time" type="text" name="start" id="start" value="{{form.timestamp}}" />

Sorry, I'm not an expert in Django. 抱歉,我不是Django专家。

除非,我对您有严重的误解,请使用val()

$('#id_timestamp').val($('#time').val())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM