简体   繁体   English

如何使用Ajax提取在“输入”字段中输入的内容?

[英]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">&#xE254;</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.

相关问题 选择项目时如何使用Ajax将数据提取到输入字段中 - How to fetch data into input field using ajax when an item is selected 在输入字段中输入网址后,使用jQuery进行Ajax调用 - Using jQuery for ajax call once url is entered in input field 如何使用jQuery和AJAX将数据从输入字段加载到另一个页面? - How do I load data from an Input Field to another page, using jQuery and AJAX? 如何使用AJAX更新simple_form输入字段? - How do I update a simple_form input field using AJAX? 如何获取使用jQuery用户输入到输入字段中的值 - How can I get the value that a user has entered into an input field using jQuery 使用ajax来获取子页面,如何更改URL? - Using ajax to fetch subpages, how do I also change the url? jQuery不允许在输入字段中输入字母 - jQuery do not allow alphabets to be entered in input field 在使用jQuery输入值后,如何通过它的值选择输入? - How do I select an input by it's value after a value is entered using jQuery? 在将ID输入到输入字段中后,从Postgresql中获取数据 - Fetch data from postgresql upon ID being entered in to input field 如果用户使用Jquery / Ajax在html输入字段上输入一些文本,该如何调用ajax? - How do I call ajax if user enter some text on html Input field with Jquery/Ajax?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM