javascript - 如何获取 Rails 表单中文本字段的新值?
问题描述
<%= form_for(@mymodel, remote: true, html: { id: 'match_form' }) do |f| %>
<%= f.text_field :match_id, style: "width:82px" %>
<%= f.submit 'Save', class: 'btn btn-primary', id: 'match_submit', style: "width:38px;padding:0px" %>
<%= button_tag 'Cancel', class: 'btn btn-secondary', id: 'match_cancel', style: "width:52px;padding:0px" %>
<% end%>
<script type='text/javascript'>
$(function() {
$(document).on("click", "#match_submit", function(event){
$.ajax('my_controller_method', {
type: 'GET',
dataType: 'script',
data: {
mid: $("#").val(), // How can I get the new value entered in 'match_id' textfield here?
},
error: function(jqXHR, textStatus, errorThrown) {
return console.log("AJAX Error: " + textStatus);
}
});
});
</script>
我有上面显示的表单和 JavaScript 块。
如何获取在 JavaScript 代码的“match_id”文本字段中输入的新值?
解决方案
您需要了解 ERB 是一个存在于服务器上的模板,然后被编译为 HTML(意思是,所有 ERB 代码完全消失),然后将生成的 HTML 发送到浏览器,然后执行 JS 代码。
JS 只会看到并使用生成的 HTML。它不知道是否存在 ERB 或 PHP 或任何用于生成其工作的 HTML 的模板语言。
您的 ERB 模板的结果将是<input id="debit_match_id">
,因此在 JS/jQuery 中选择它的正确方法是$("#debit_match_id")
.
推荐阅读
- css - 基于内容高度的 CSS-Grid 填充行
- javascript - 如何在多个表行中通过选择(mysql数据库)获取动态值
- python - 如何在python中检测句子的语言
- firebase - Flutter Firestore 错误:DocumentSnapshotPlatform 中不存在字段
- dart - dart CLI 命令 - 为什么没有 lib/proj.dart 文件?飞镖甜点
- leaflet - 在 Leaflet 中的路线上添加标签
- java - (java.nio.file.NoSuchFileException) 在我的代码中?
- python - 将 POST 请求从客户端发送到 Django 模型的最佳实践?
- javascript - SetTimeout 不同步时如何同步?
- python - 如何使用 f 字符串从多数组字典(Python)中获取信息?