【问题标题】:How to extract the id of a Django formset in Javascript?如何在 Javascript 中提取 Django 表单集的 id?
【发布时间】:2022-01-04 05:42:45
【问题描述】:

我怀疑发生了什么是我试图在 Javascript 中提取 Django 表单集的 id,但是当我尝试这样做时,我意识到这出现在 Chrome DevTools 中:

S.fn.init

[[Prototype]]: Object (0)

也就是说,什么都没有发生,但这对我来说似乎很奇怪,因为在控制台中打印时,我得到了我想要的 id 名称

发生了什么,我如何提取它的 id 和值??

计算.js


//the number of total forms is taken

var formId="id_form-TOTAL_FORMS"
var form_idx = document.getElementById('id_form-TOTAL_FORMS').value;
console.log("el valor es:" + form_idx);
var totalForms=parseInt($('#'+formId).val());
console.log("totalForms:"+totalForms)
console.log(totalForms+1);

//dynamic id are created

var formset_quantity='id_form-'+form_idx+'-quantity';
var formset_unit_price='id_form-'+form_idx+'-unit_price';
console.log('formset quantity: '+formset_quantity);
console.log('formset unit price: '+formset_unit_price);


//the value of the one that contains the dynamic id is taken

//This is when everything goes wrong because when printing in the console I get the error above,
// as if that id does not exist(formset_quantity and formset_unit_price) or I am not creating it correctly

var id_formset_quantity=document.getElementById('formset_quantity').value;
var id_formset_unit_price=document.getElementById('formset_unit_price').value;
var formset_total_price=document.getElementById('id_form-'+form_idx+'-total_price');


presupuestos-forms.html


td id="parent_id_form-0-quantity">
  <input type="number" name="form-0-quantity" class="form-control" id="id_form-0-quantity">
</td>
<td id="parent_id_form-0-unit_price">
   <input type="number" name="form-0-unit_price" class="form-control" id="id_form-0-unit_price">
</td>
<td id="parent_id_form-0-total_price">
   <input type="number" name="form-0-total_price" class="form-control" id="id_form-0-total_price">
</td>

【问题讨论】:

    标签: javascript python html django


    【解决方案1】:

    引用变量时省略引号。否则将被解释为字符串。

    var id_formset_quantity=document.getElementById(formset_quantity).value;
    

    【讨论】:

      猜你喜欢
      • 2012-03-27
      • 2010-11-27
      • 2018-10-29
      • 1970-01-01
      • 2011-04-15
      • 2018-02-09
      • 1970-01-01
      • 1970-01-01
      • 2015-03-19
      相关资源
      最近更新 更多