【发布时间】:2020-03-02 07:53:58
【问题描述】:
我对 javascript 很陌生,并且还在尝试 fetch api 与后端交互。我已经制作了一些复选框,如果它们被选中,我想将它们的值存储在一个数组中(以 json 格式),以及将在输入框中输入的值,然后使用 fetch api 将该数组发送到后端,以便它可以用来做处理。任何人都可以在这里帮助我。
$("#step1").click(function() {
$("#checked1").css("display", "initial");
$("#checked1").toggle($(this).prop("checked"));
});
$("#step2").click(function() {
$("#checked2").css("display", "initial");
$("#checked2").toggle($(this).prop("checked"));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<p> Ready to start </p>
<div class="form-check checkbox-rounded checkbox-filled">
<input type="checkbox" class="form-check-input filled-in" value="Yes" name="demo" id="Yes">
<label class="form-check-label" for="Yes">Yes</label>
</div>
<div class="form-check checkbox-rounded checkbox-filled">
<input type="checkbox" class="form-check-input filled-in" value="step1" name="demo" id="step1">
<label class="form-check-label" for="step1">Check to start step1</label>
<div id ="checked1" style="padding-left:20px; display: none;" class="custom-control-inline">
<input type="text" id="inputValue" class="form-control form-centrol-sm" style="width: 60%; display: inherit;" placeholder="Enter your name">
<hr style="margin-top: 0.5rem !important; margin-bottom: 0.5rem !important;">
</div>
</div>
<div class="form-check checkbox-rounded checkbox-filled">
<input type="checkbox" class="form-check-input filled-in" value="step2" name="demo" id="step2">
<label class="form-check-label" for="step2">Check to start step2</label>
<div id ="checked2" style="padding-left:20px; display: none;" class="custom-control-inline">
<input type="text" id="inputValue" class="form-control form-centrol-sm" style="width: 60%; display: inherit;" placeholder="Enter your age">
<hr style="margin-top: 0.5rem !important; margin-bottom: 0.5rem !important;">
</div>
</div>
【问题讨论】:
-
既然你用的是jQuery,有没有查serialize?。做一些研究,然后尝试一下。当您的努力似乎徒劳无功时,请返回此处询问您的代码为什么不起作用或您可能做错了什么。您将学到更多,我们也更容易为您提供帮助。
-
你已经在使用jQuery了,为什么不直接使用
$.ajax呢?为什么必须是 Fetch API?
标签: javascript jquery html json fetch-api