[英]How do I fetch the contents entered in the Input field using Ajax?
I'm having an input field in the form where the users updates their Name on click of a button. 我在表单中有一个输入字段,用户在其中单击按钮即可更新其名称。 This change in the input field should be captured by the Ajax and update it. 输入字段中的此更改应由Ajax捕获并进行更新。
Html - HTML-
<p id="name"><%= @current_user.name %></p>
<button type="button" id="namebtn"></i> </button>
coffee Script - 咖啡脚本-
$('form').on 'focusout', ->
$elem = $(this)
if !$elem.find(':focus').length
$.ajax({
type: "POST",
url: "http://localhost:3000/profile/name_change",
data: "{
'name': '"+$('#name').val()+"'}",
})
return
How do I capture the changed input from the input field? 如何从输入字段捕获更改的输入?
You say you have an 'input field', yet #name
is clearly a p
element. 您说您有一个“输入字段”,但是#name
显然是p
元素。 You need to use text()
to get its value in that case. 在这种情况下,您需要使用text()
来获取其值。
Also note that your use of focusout
on the form whilst checking the :focus
element is rather odd. 还要注意,在检查:focus
元素时,在窗体上使用focusout
很奇怪。 You could just use a standard click
handler instead: 您可以只使用标准的click
处理程序:
$('#namebtn').on 'click', ->
$.ajax({
type: "POST",
url: "http://localhost:3000/profile/name_change",
data: { name: $('#name').text() }
})
return
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.