简体   繁体   English

Django HTML,如何通过 AJAX 传递单选选项选择并加载表单

[英]Django HTML, how to pass radio option select via AJAX and load form

how to pass value selected from radio button to ajax url.如何将从单选按钮中选择的值传递给 ajax url。 I have radio button select download/upload.我有单选按钮选择下载/上传。 CODE:代码:

  <form id="listofiles" action="" class="post-form" role=form method="post">{% csrf_token %}
  Select: Download:
  <input class="form-check-input" name="optionsRadios" type="radio" value="download">
  or Upload:
  <input class="form-check-input" name="optionsRadios" type="radio" value="upload">
  BUTTON: 
  <input type="submit" value="GO" id="download" name="download" class="btn btn-info" />
  <input type="submit" value="GO" id="upload"  name="upload" class="btn btn-warning" />

Based on which one is select button will show.根据哪个是选择按钮将显示。 CODE:代码:

<script>
$("input[name='optionsRadios']:radio")
  .change(function() {
    $("#upload").toggle($(this).val() == "upload");
    $("#download").toggle($(this).val() == "download"); });
</script>

Once the user selects the options, it will load the data from the other HTML file into div CODE:一旦用户选择了选项,它就会将其他 HTML 文件中的数据加载到 div CODE 中:

<div id="fetchdata" align="center">
  <!-- LOADING DATA FROM THE AJAX listofiles.html -->
</div>

AJAX CODE:阿贾克斯代码:

$("#listofiles").submit(function() { // catch the form's submit event
$.ajax({ // create an AJAX call...
    url: 'listofiles.html',
    type: $(this).attr('GET'),
    data: $(this).serialize(), // get the form data
    success: function(data) { // on success..
        $("#fetchdata").html(data); // update the DIV
        console.log(data);
    }
});
return false;
});

HTML: listofiles.html Issue, in this page, I have two forms with the different ID. HTML: listofiles.html 问题,在这个页面中,我有两个不同 ID 的表单。 How to load forms based on the optionsRadios selected.如何根据所选的选项Radios加载表单。

CODE:代码:

   <div id="download" style="display:none"><div align="center" class="container">
   <form id="download" action="download" role=form method="POST" class="post-form">{% csrf_token %}
   . . . 
   <div class="col" align="left">
     <button type="submit" name="download" class="btn btn-primary btn-lg">DOWNLOAD</button>
   </div></div></form></div></div>

   <div id="upload" style="display:none"><div align="center" class="container">
   <form id="upload" action="upload" role=form method="POST" class="post-form">{% csrf_token %}
   . . . 
   <div class="col" align="left">
     <button type="submit" name="upload" class="btn btn-primary btn-lg">UPLOAD</button>
   </div></div></form></div></div>

I am assuming that we stay on the same page: then we can update your code:我假设我们保持在同一页面上:然后我们可以更新您的代码:

Reuse the same selector:重用相同的选择器:

$("input[name='optionsRadios']:radio:checked").val() == "upload");

Use the checked pseudoselector to see which value was selected to toggle the correct div.使用checked伪选择器查看选择了哪个值来切换正确的 div。

Executing this code will result in multiple elements with the same id name.执行此代码将产生多个具有相同 id 名称的元素。 Better to use class names or unique ids.最好使用类名或唯一 ID。

$("#listofiles").submit(function() { // catch the form's submit event
$.ajax({ // create an AJAX call...
    url: 'listofiles.html',
    type: $(this).attr('GET'),
    data: $(this).serialize(), // get the form data
    success: function(data) { // on success..
        $("#fetchdata").html(data); // update the DIV
        $("div[id='upload']").toggle($("input[name='optionsRadios']:radio:checked").val() == "upload");
        $("div[id='download']").toggle($("input[name='optionsRadios']:radio:checked").val() == "download");  
//there is already another element with id download | you need to change that, so circumventing like this for now.
        }
    }
});
return false;
});

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

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