Javascript 按id获取jQuery表单-文本值
我有一个简单的html和jQuery表单,我想: 1) 将值放入js变量中。 2) 通过JSON将其发送到服务器。 表格如下:Javascript 按id获取jQuery表单-文本值,javascript,jquery,html,ajax,json,Javascript,Jquery,Html,Ajax,Json,我有一个简单的html和jQuery表单,我想: 1) 将值放入js变量中。 2) 通过JSON将其发送到服务器。 表格如下: <div data-role="main" class="ui-content"> <data=role "form" name="seekerForm" id="jobSeekerForm" > <label for="basic">First name :&
<div data-role="main" class="ui-content">
<data=role "form" name="seekerForm" id="jobSeekerForm" >
<label for="basic">First name :</label>
<input type="text" name="seekerName" id="WfirstName" data-mini="true" />
<label for="basic">Last name :</label>
<input type="text" name="seekerLast" id="WlastName" data-mini="true" />
<label for="basic" id="WEmail">E-mail:</label>
<input type="text" name="seekerEmail" id="WEmail" data-mini="true" />
<div data-role="fieldcontain" >
<label for="select-choice-1" class="select" >Choose a field:</label>
<select name="select-choice-1" id="selectField" >
<option value="HI-TEC">Software Programming</option>
<option value="webPro">Web Programming</option>
<option value="QA">QA</option>
<option value="systemInfo">System Information</option>
<option value="DB">DBA</option>
<option value="java">JAVA</option>
<option value="c">c++</option>
<option value="pyt">pyton</option>
</select>
</div>
<div data-role="fieldcontain" >
<label for="select-choice-1" class="select" >Choose Experience level:</label>
<select name="select-choice-1" id="selectPro" >
<option value="junior">junior(0-2)</option>
<option value="senior">senior(2-5)</option>
<option value="exp">Expert(5-10)</option>
</select>
</div>
</form>
您可以使用获取所有表单值,并通过ajax将它们发送到服务器。这样做可以:
$(function() {
$('form').on('submit', function( e ) {
e.preventDefault();
$.ajax( url:'http://localhost:8888/', data: $(this).serialize(), ..... );
});
});
您的页面必须在相同的
localhost
相同的端口8888
上运行才能正常工作。您可能需要签出用于获取表单值的jQuery.serialize()
方法。
这是我的代码:$(function(){var workerInfo=$(“#workerForm”).serialize();$(“#jobRes”).html(workerInfo););但是它没有得到值:seekerName=&seekerLast=&seekerEmail=&select-choice-1=HI-TEC&select-choice-1=junior&slider-1=40尝试用
替换数据=角色“表单”
。所有内容都必须在试图序列化的表单标记内。是否可以将ID?添加到
元素?当然这是我的代码:$(function(){var workerInfo=$(“#workerForm”).serialize();$(“#jobRes”).html(workerInfo);)};但是它没有得到值:seekerName=&seekerLast=&seekerEmail=&select-choice-1=HI-TEC&select-choice-1=junior&slider-1=40这是因为在填充表单之前,页面一加载,此代码就会运行。获取值的代码属于提交处理程序,如我提供的示例所示。提交表单时会执行提交处理程序——希望在填写表单并单击提交按钮后执行。我使用:$(“#GoButton”)。单击(函数(){..}。谢谢你,你帮了我很多。我应该把它改成表单标签吗?还是改成提交按钮?不客气。提交表单时,总是建议使用表单上的submit
事件,而不是某个按钮或提交按钮上的click
事件。我会使用$('form-selector')。提交(…)
。这太好了!TY!!你知道如何用json连接那个字符串吗?
$(function() {
$('form').on('submit', function( e ) {
e.preventDefault();
$.ajax( url:'http://localhost:8888/', data: $(this).serialize(), ..... );
});
});