jQuery OnClick 发送到 Ajax

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16754246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:42:03  来源:igfitidea点击:

OnClick Send To Ajax

javascriptjqueryhtmlajax

提问by Sophie Mackeral

I'm trying to complete some ajax requests to insert a textarea into a database without refresh. Here is my code:

我正在尝试完成一些 ajax 请求,以在不刷新的情况下将 textarea 插入到数据库中。这是我的代码:

HTML:

HTML:

<textarea name='Status'> </textarea>
<input type='button' onclick='UpdateStatus()' value='Status Update'>

JS:

JS:

function UpdateStatus(Status)
    {
    var Status = $(this).val();

        $(function()
        {
            $.ajax({
                url: 'Ajax/StatusUpdate.php?Status='.Status, data: "", dataType: 'json'
            });

        });
    }

My Questions:

我的问题:

1) How do I send the contents of the text area into the onclick function?

1)如何将文本区域的内容发送到onclick函数中?

2) How do I escape/urlencode etc.. So it retains line breaks

2)我如何转义/ urlencode等。所以它保留换行符

回答by Mohammad Adil

<textarea name='Status'> </textarea>
<input type='button' value='Status Update'>

You have few problems with your code like using .for concatenation

您的代码几乎没有问题,例如.用于连接

Try this -

尝试这个 -

$(function () {
    $('input').on('click', function () {
        var Status = $(this).val();
        $.ajax({
            url: 'Ajax/StatusUpdate.php',
            data: {
                text: $("textarea[name=Status]").val(),
                Status: Status
            },
            dataType : 'json'
        });
    });
});

回答by Rajeev RK

Tried and working. you are using,

尝试和工作。您正在使用,

<textarea name='Status'> </textarea>
<input type='button' onclick='UpdateStatus()' value='Status Update'>

I am using javascript , (don't know about php), use id ="status" in textarea like

我正在使用 javascript ,(不知道 php),在 textarea 中使用 id ="status"

<textarea name='Status' id="status"> </textarea>
<input type='button' onclick='UpdateStatus()' value='Status Update'>

then make a call to servlet sending the status to backend for updating using whatever strutucre(like MVC in java or anyother) you like, like this in your UI in script tag

然后调用 servlet 将状态发送到后端以使用您喜欢的任何结构(如 Java 中的 MVC 或任何其他结构)进行更新,就像在脚本标记中的 UI 中这样

<srcipt>
function UpdateStatus(){

//make an ajax call and get status value using the same 'id'
var var1= document.getElementById("status").value;
$.ajax({

        type:"GET",//or POST
        url:'http://localhost:7080/ajaxforjson/Testajax',
                           //  (or whatever your url is)
        data:{data1:var1},
        //can send multipledata like {data1:var1,data2:var2,data3:var3
        //can use dataType:'text/html' or 'json' if response type expected 
        success:function(responsedata){
               // process on data
               alert("got response as "+"'"+responsedata+"'");

        }
     })

}
</script>

and jsp is like

而jsp就像

the servlet will look like:   //webservlet("/zcvdzv") is just for url annotation
@WebServlet("/Testajax")

public class Testajax extends HttpServlet {
private static final long serialVersionUID = 1L;
public Testajax() {
    super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    String data1=request.getParameter("data1");
    //do processing on datas pass in other java class to add to DB
    // i am adding or concatenate
    String data="i Got : "+"'"+data1+"' ";
    System.out.println(" data1 : "+data1+"\n data "+data);
    response.getWriter().write(data);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    doGet(request, response);
}

}

}