开发者

Pulling form data with Javascript (with a bit of ReST)

开发者 https://www.devze.com 2023-04-03 11:11 出处:网络
I know this has probably been asked a dozen times, but I am kind of stumped. I have a form: <form id=\"login\">

I know this has probably been asked a dozen times, but I am kind of stumped.

I have a form:

<form id="login">

<label>Email</label>
<input id="email"/>


<label>First Name</label>
<input id="fName"/>


<label>Last Na开发者_C百科me</label>
<input id="lName"/>


<label>Phone #</label>
<input id="phone"/>

And the ReST call I am using is:

<script type="text/javascript">

$.ajax({
    url: 'http://example.com/',
    type: 'PUT',
    data: 'ID=1&fname=datagoeshere&lname=datagoeshere&email=datagoeshere', 
    success: function() { alert('PUT completed'); }
});


</script>

I guess the part I am stuck on is how to pull out just fname,lname and email and stick the information that gets entered into the form into the ReST call. The form is also posting to another form.


Replace:

'ID=1&fname=datagoeshere&lname=datagoeshere&email=datagoeshere'

with

{ ID: 1, fname: $('#fname').val(), lname: $('#lanme').val(), email: $('#email').val() }

jQuery's ajax data property takes an object and will format that object into the proper string for you.

0

精彩评论

暂无评论...
验证码 换一张
取 消