简体   繁体   English

使用 AJAX 选择下拉菜单选择器时遇到问题

[英]Trouble selecting drop-down menu selector using AJAX

Currently I'm trying to create a page where my database is updated upon a drop event.目前,我正在尝试创建一个页面,在该页面中,我的数据库会根据放置事件进行更新。 More specifically, I have a page with drag-drop capabilities, what I need is anytime I drag and drop a particular div (deal-card) into a another div (stage-column), I need to make update to the database, that represents that "deal" placed in a different "stage".更具体地说,我有一个具有拖放功能的页面,我需要的是任何时候将特定的 div(交易卡)拖放到另一个 div(阶段列)中,我需要更新数据库,即表示置于不同“阶段”的“交易”。

For this project my backend is Django, and up to this point, I'm able to successfully change the basic CharFields on a drop event, though selecting a selector in the drop-down menu has me stumped.对于这个项目,我的后端是 Django,到目前为止,我能够在放置事件中成功更改基本的 CharFields,尽管在下拉菜单中选择一个选择器让我感到难过。

As of now I just arbitrarily picked "value" = 3 for the targeted selector, just to see if I could get it to work.截至目前,我只是随意为目标选择器选择了“值”= 3,只是为了看看我是否能让它工作。 Unfortunately, whatever I try, I always get a prompt saying "This field is required".不幸的是,无论我尝试什么,我总是会收到提示“此字段为必填项”。 I attached a picture showing this below.我在下面附上了一张图片。

Any help would be greatly appreciated!任何帮助将不胜感激!

AJAX CODE AJAX 代码

$(document).one('drop', function(ev){
            ev.preventDefault();

            var stage_title_path = '#' + el.id + " .stage-title";
            var stage_title = $(stage_title_path).html();
            
            $.ajax({
                type:'POST',
                url:deal_card_url,
                data:{
                    deal_owner: 'Sally Sue',
                    deal_address: '745 Amen Street',
                    deal_arv: '222',
                    deal_repair: '333',
                    
                    csrfmiddlewaretoken:$('input[name=csrfmiddlewaretoken]').val(),
                },
                success:function(){
                    $("#id_stage").val("3").change();
                }
            });

        });
        

FORM DROP-DOWN HTML表格下拉 HTML

<p><label for="id_stage">Stage:</label> <select name="stage" required="" id="id_stage">
  <option value="">---------</option>

  <option value="1" selected="">New</option>

  <option value="2">Sent Offer</option>

  <option value="3">Under Contract</option>

  <option value="4">Option</option>

  <option value="5">Closed</option>

  <option value="24">New Lead</option>

</select></p>

Ajax Updated Deal Update Webpage: Ajax 更新交易更新网页:

Figured it out.弄清楚了。 It was actually quite simple...其实很简单...

For the "data" setting in the ajax function I needed to specify the "value" of the desired selector (aka stage), as shown in the code below.对于 ajax function 中的“数据”设置,我需要指定所需选择器(也称为阶段)的“值”,如下面的代码所示。

data:{
     deal_owner: 'Sally Sue',
     deal_address: '745 Amen Street',
     deal_arv: '222',
     deal_repair: '333',
     stage: "2",

In this case passing a stage "value" equal to "2", as seen in the html, corresponds to the "Sent Offer" option in the drop-down menu.在这种情况下,传递等于“2”的阶段“值”,如 html 中所示,对应于下拉菜单中的“已发送报价”选项。 Previously I tried to use the actual name of the selector (eg. Sent Offer).以前我尝试使用选择器的实际名称(例如 Sent Offer)。

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

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