- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
好的,使用 Scala Play Framework 我的应用程序 Controller 中有一个 Action 方法,它访问特定序列的数据库并使用 Ok(json) 发送一个 JSON 对象,可以通过路由 GET/json 访问/:串行 Controller .Application.getData(串行:字符串)
。这部分有效;我能够直接通过 localhost/json/serial# 检索特定序列号的未格式化 JSON 对象。
在我看来,有一个页面可以编辑这个数据库信息。 There is an HTML select drop down with a list of serial numbers, and when one of them is selected I'd like to send an AJAX request with that serial number to/json/### and get a JSON object in return.一旦我有了 JSON 对象,就应该用该信息填充表单,以便用户知道他们正在编辑什么。
所以基本上我的问题是如何使用 AJAX/jQuery 通过 HTTP(使用路由/json/:serial)请求 JSON 对象,以便在序列化时我可以使用 JSON 对象中的各个字段填充 HTML 表单是从下拉列表中选择的?
如果有任何不清楚或太含糊的地方,请告诉我!谢谢
编辑:一些代码:
val getData(serial: String) = Action {
val scInfo = *some database query*
val ctrlInfo = *another database query*
val json: JsValue = Json.obj(
"name" -> scInfo(0)._1,
"notes" -> scInfo(0)._2,
"ctrl1" -> Json.obj("name" -> scInfo(0)._3,
"port" -> scInfo(0)._4,
"apc" -> scInfo(0)._5),
"ctrl2" -> Json.obj("name" -> ctrlInfo(0)._1,
"port" -> ctrlInfo(0)._2,
"apc" -> ctrlInfo(0)._3),
"rack" -> scInfo(0)._6,
"comtrol" -> scInfo(0)._7
)
Ok(json)
}
这是表单的大致外观图片(我认为查看实际页面比阅读标记更容易)
So when a Storage Center is selected, that number is sent via AJAX to /json/:serial
and should request a JSON object as a result, which will then populate the other fields in the form .
编辑 2:
这是 View 的标记。它使用 Play 模板引擎,所以任何在它前面带有 @
的东西都只是 scala 代码生成标记。
<div class="header-wrapper">
<h2 class="header">Edit Information for...</h2>
</div>
<div class="sc-content">
<form class="form-horizontal col-md-12" role="form">
<div class="form-group">
<label for="inputSC" class="control-label">Storage Center</label><br>
<div class="col-sm-6">
<select class="form-control" onfocus="this.blur()" id="inputSC" placeholder=" ">
@storageCenters.map { sc =>
<option id="selectSC@sc._1">@{sc._1.replace('-', '/')}</option>
}
</select>
</div>
</div>
<div class="form-group">
<label for="inputRack" class="control-label">Rack</label><br>
<div class="col-sm-6">
<select class="form-control" onfocus="this.blur()" id="inputRack" placeholder=" ">
@racks.map { rack =>
<option id="selectRack@rack.name">@rack.name</option>
}
</select>
</div>
</div>
<div class="form-group">
<label for="inputConsoleIP" class="control-label">Console IP/Port</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="inputConsoleIP" placeholder="i.e. 127.0.0.1">
</div>
</div>
<div class="form-group">
<div class="col-sm-10">
<input type="text" class="form-control" id="inputPort" placeholder="i.e. 00000">
</div>
</div>
<div class="form-group">
<label for="inputAPC" class="control-label">APC Outlet</label><br>
<div class="col-sm-6">
<input type="text" class="form-control" id="inputAPC" placeholder="i.e. 12">
</div>
</div>
<div class="form-group">
<label for="inputNotes" class="control-label">Notes</label><br>
<div class="col-sm-12">
<input type="text" class="form-control" id="inputNotes" value="Default">
</div>
</div>
<div class="form-group">
<div style="margin-lefT:4%;">
<button type="submit" class="btn btn-default">Submit Changes</button>
</div>
</div>
</form>
</div>
最佳答案
一位 friend 帮助了我,我想出了一个答案!正如预期的那样,使用 jQuery 很简单。
前端:
唯一发生变化的是 HTML select 标签获得了一个 onchange="fillForm()"
属性,该属性会在序列号被选中时调用 JS 函数。
<select class="form-control" onfocus="this.blur()" id="inputSC" onchange="fillForm()">
后端:
我创建了一个 JS 文件,其中包含与此表单相关的多个函数,尤其是 fillForm()
函数。
function fillForm() {
var val = $('#inputSC').val() // selected value
val = val.replace('/', '-');
$.getJSON('/json/' + val, function (data) {
//printJSON(data);
$('#inputRack').val(data.rack);
$('#inputNotes').val(data.notes);
$('#inputComtrol').text(data.comtrol);
$('#inputPort').val(data.ctrl1.port);
$('#inputAPC').val(data.ctrl1.apc);
});
}
而且对于JavaScript/jQuery不是很好的人来说,这个函数首先获取下拉select标签的值(注意在页面下拉选择一个新的值会触发这个函数) 然后格式化字符串 (特定于我的应用程序的内容)。然后使用 jQuery,它调用 $.getJSON(url [, data] [,success]);
(here's the documentation)从我已经在路由中设置的 URL 检索特定于序列的 JSON,然后将表单中每个字段的值更改为 JSON 中的数据。在我看来,将$.getJSON
签名想象成$.getJSON(url, callback);
比较容易,但是要引用官方的文档。
成功了!
请随时评论我会尝试回答的问题!
关于jquery - 如何通过 AJAX 到 HTTP 请求 JSON 来预填充 Web 表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24616993/
我在 mongodb 中的玩家和锦标赛之间存在多对多关系。 我希望能够一次将许多玩家添加到锦标赛中。如果没有 ajax,这很简单,但我们有一个包含数千名玩家的数据库,因此表单选择变得巨大。 我们想为此
这个问题已经有答案了: When should I use html's and when spring's in Spring MVC web app? (3 个回答) 已关闭 6 年前。 我正
我正在 C++ Builder XE4 上使用 VCL。 我有以下组件。 FormMain 具有 TButton *B_select; FormSelect(或DialogSelect)具有 TCom
如何在不影响表单控件的情况下更改表单的 alphablend? 德尔福XE7 最佳答案 此问题的一个解决方案是使用多设备应用程序(如果无法使用VCL)。 如果您需要保留透明的TForm,只需更改属性T
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我正在尝试扩展 Django 注册以包含我自己的注册表单。原则上这是相当简单的。我只需要编写自己的表单( CustomRegistrationForm ),它是原始表单( RegistrationFo
我正在尝试为我的网站实现聊天功能。为了做到这一点,我遵循了以下教程:https://channels.readthedocs.io/en/latest/tutorial/ 然后我稍微更改了代码以实现它
有一个问题,我需要用一个 html 表单提交两个相互关联的模型表单。我知道如何提交两个单独的表格,但是在相关模型表格的情况下外键让我发疯。 问题是,第二个表单应该用外键填充字段到第一个表单的实例。 在
我正在创建一个工具,允许某人输入食谱,然后将其保存为 XML 文件,我已经创建了 XSD,但我想知道如何在我的网页上制作一个表单以允许用户输入他们的食谱并遵守模式。我一直在研究 Ajax 和 Jque
在 .net win 表单(如 asp.net web 表单)中是否有可用的验证控件? 因为很难为我的每个控件设置正确的条件,所以我的表单中也有很多重复的代码。 正确的做法是什么? 最佳答案 看看这个
我有一个简短的问题。我正在学习如何使用 javascript 制作注册表,发现此链接非常有用。 http://www.w3resource.com/javascript/form/javascript
我正在开发一个项目,该项目将使用循环将许多表单添加到 mysql 数据库中。在 javascript 部分中,我无法让 var i 在函数 updatesum() 中工作。有人可以帮我吗? 我试图避免
在我的应用程序上有一个包含 2 个字段和一个保存按钮的表单。 在我的 onClick 结束时我需要什么来将光标返回到第一个字段。 我有这个来清除它们 txtData.setText("
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
<input type="text" name="textfield" onKeyPress="javascript:alert(event.
我正在构建的网站有一个登录表单,作为所有其他模板扩展的 base.html 模板的一部分;因此,我需要以某种方式处理每个页面上的登录/注销逻辑。 目前每个页面都在单独的 View 中加载,那么实现它的
我有一个表单类,看起来像.. #forms.py class ExampleForm(forms.Form): color = forms.CharField(max_length=25)
有没有办法在表单定义中给表单一个特殊的错误渲染函数?在 customizing-the-error-list-format 下的文档中它展示了如何为表单提供特殊的错误呈现函数,但似乎您必须在实例化表单
我正在处理由多个页面组成的表单,我想解决验证问题。 当我点击提交按钮时,当前页面上的所有字段都会在下方显示错误消息,但是如果我更改页面,那么我需要再次点击提交,因为这些字段未设置为已触摸。 如果我可以
是否可以附加到继承表单的 exclude 或 widgets 变量? 到目前为止,我有以下设置。 class AddPropertyForm(forms.ModelForm): num_mon
我是一名优秀的程序员,十分优秀!