jquery - How to use a javascript to validate a form? -


i trying build django form. , javascript logic trying implement if input form value not empty, submit form. however, not sure how id of {{form.value}}.

    <form id = "form1" action="." method="post">{% csrf_token %}          <div class=".col1" style="float:left;vertical-align: middle;">                    {{ form.region }}          {{ form.operator }}     </div>      <div class="col-xs-2 style="vertical-align: middle;">         {{ form.value }}     </div>      </form>   function submitform1(){  if(document.getelementbyid( {{form.value}} ).value != ""){     document.getelementbyid("form1").submit(); }  } 

the source of problem mixing code in django template, processed on server side, code doing in javascript, processed on client (browser) side. variable names exist on server side not exist in same way in browser. in javascript can work elements template has rendered html output sent browser.

in case of django form, form fields rendered id of id_ + the field name. so:

document.getelementbyid("id_region").value document.getelementbyid("id_operator").value document.getelementbyid("id_value").value 

..etc, give values of form fields.

if use prefix= argument form constructor in view, eg:

myform = myformclass(prefix='form1') 

then prefix (form1), followed dash -, prepended field name of fields in form:

 document.getelementbyid("id_form1-value").value  

if not sure of name or id of field in form, can 'view source' or 'inspect element' in browser see what's in html document.


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -