我在 HTML 表單中有這個簡單的隱藏欄位:
<input type="hidden" id="response" name="response">{{ form.response}}</div>
我想更改它的值,以便以后可以使用燒瓶和 WTForms 使用它。
我試過這個:
function(token){
document.getElementById('response').value = token
}
并且正在使用有效令牌呼叫該函式,但沒有成功。
有什么建議么?
uj5u.com熱心網友回復:
表單的輸入欄位創建如下,其中可能有標簽或驗證器等附加引數。
class ExampleForm(FlaskForm):
response = HiddenField()
submit = SubmitField()
在服務器端請求值需要以下代碼。
@app.route('/', methods=['GET', 'POST'])
def index():
form = ExampleForm(request.form)
if form.validate_on_submit():
print(form.response.data)
return render_template('index.html', **locals())
如果您現在在模板中渲染,屬性名稱和 id 會自動設定。該值對應于分配輸入欄位的變數的識別符號。要查詢和設定值,您可以使用帶有 name 屬性或輸入欄位 id 的選擇器。
<form method="post">
{{ form.csrf_token }}
{{ form.response }}
{{ form.submit }}
</form>
<script type="text/javascript">
(() => {
const token = 'your token here';
let responseField;
// Selecting the input field based on the id attribute,
responseField = document.getElementById('response');
responseField.value = token;
// or select based on the name attribute.
let responseField = document.querySelector('input[name="response"]');
responseField.value = token;
})();
</script>
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/463885.html
標籤:javascript 烧瓶 wtforms
