如何通过Ajax调用表单的一部分?
我有一张表格,里面有很多盒子。我想添加一个复选框字段,因为值来自通过 Ajax 调用的 php 文件。如何在一个表单中处理另一个表单?
<form action="result.php" method="post">
<input ...
-------Inner form (imaginary form to perform Ajax call)
TYPE YOUR COUNTRY AND POPULATE CITY
(here user types a country and we populate cities from city.php?q=typed-country.
City will return by the output of city.php file. In other words, output of
city.php is exactly list of cities, which can be formatted with required
CHECKBOX codes)
List of cities as CHECKBOX; e.g.
<input type="checkbox" name="city" value="city1" />
<input type="checkbox" name="city" value="city2" />
<input type="checkbox" name="city" value="city3" />
--------
<input type="submit" value="Submit" />
</form>
有两个选择字段的示例,用于捕获所选国家/地区的城市;但在这里我需要国家的类型输入和生成的城市的复选框。
I have a form with lots of boxes. I want to add a field of checkbox as the values come from a php file via Ajax call. How to handle a form within another form?
<form action="result.php" method="post">
<input ...
-------Inner form (imaginary form to perform Ajax call)
TYPE YOUR COUNTRY AND POPULATE CITY
(here user types a country and we populate cities from city.php?q=typed-country.
City will return by the output of city.php file. In other words, output of
city.php is exactly list of cities, which can be formatted with required
CHECKBOX codes)
List of cities as CHECKBOX; e.g.
<input type="checkbox" name="city" value="city1" />
<input type="checkbox" name="city" value="city2" />
<input type="checkbox" name="city" value="city3" />
--------
<input type="submit" value="Submit" />
</form>
There are example of two select field for capturing cities for selected country; but here I need TYPE INPUT for country and CHECKBOX for resulting cities.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用上面的代码,您的查询将返回城市列表,每个城市之间用逗号分隔。在 HTML 中,您有一个 div,其 id 为 cityListContainer。
您还可以使用appendChild方法来添加每个单独的复选框,但我的是懒人的答案。
演示
Use the above code where your query returns a list of cities, with each city being separated by a comma. In your HTML, you have a div, with the id citiesListContainer.
You could also use the appendChild method to add each individual checkbox, but mine is the lazy man's answer.
Demo
表单中不能有表单。您可以提交整个表单(并且仅使用必要的数据)或分开两个表单。
You can't have a form inside of a form. You can make the entire form submit (and just use necessary data) or separate the two forms.
在国家/地区上使用 .change
,例如用于选择框,但是使用 .change 的原则对于任何类型都是相同的,尽管您不需要选择选项:selected.each 位
use .change on the country
that would example would be for a select box, however the principle of using .change would be the same for any type, though you wouldn't need the select option:selected.each bit