使用jquery访问表单中的值时出现问题
我正在编写一个工具,它接受用户输入的地址,并将其转换为谷歌地图api的地理位置 编辑:我确实想提交表单数据。。我想。我不确定是否有必要提交表单,但最终结果是它应该在wordpress中生成一个包含使用jquery访问表单中的值时出现问题,jquery,forms,wordpress,geocoding,Jquery,Forms,Wordpress,Geocoding,我正在编写一个工具,它接受用户输入的地址,并将其转换为谷歌地图api的地理位置 编辑:我确实想提交表单数据。。我想。我不确定是否有必要提交表单,但最终结果是它应该在wordpress中生成一个包含标题,地址,和地理值的帖子 如果删除了标记,则submit按钮调用的方法将正常工作。如果标记在表单周围,则仍将调用该方法,但不会对任何内容进行地理编码。我的表格设置不正确吗 表格如下: $title = $_POST['title']; $address = $_POST['address']; $n
标题
,地址
,和地理
值的帖子
如果删除了
标记,则submit按钮调用的方法将正常工作。如果
标记在表单周围,则仍将调用该方法,但不会对任何内容进行地理编码。我的表格设置不正确吗
表格如下:
$title = $_POST['title'];
$address = $_POST['address'];
$new_post = array(
'post_title' => $title,
'post_content' => 'This is my post.',
'post_status' => 'publish',
'post_type' => 'post',
'address' => $address,
'geo' => $geo,
);
$post_id = wp_insert_post($new_post);
update_post_meta($post_id, 'address', $address, true);
update_post_meta($post_id, 'geo', $geo, true);
?>
<?php get_header()?>
<label>Event: </label><input id="title" type="text"/>
<label>Address: </label><input id="address" type="text"/>
<div id="map_canvas" style="width:500px; height:500px"></div><br/>
<label>latitude: </label><input id="latitude" type="text"/><br/>
<label>longitude: </label><input id="longitude" type="text"/><br/>
<input type="submit" id="compute" onClick="getCoordinates()" value="lets try it">
<input type="hidden" name="action" value="new_post" />
<?php wp_nonce_field( 'new-post' ); ?>
<?php get_footer()?>
将输入按钮从type=“submit”更改为type=“button”,并关闭表单标签。当您想要实际提交表单时,您只能使用内部带有提交按钮的表单,而不是只执行javascript操作。在使用元素时,最好将此地理编码函数绑定到
<form onsubmit="getCoords()">
...fields...
</form>
<script>
function getCoords(){
geocoder.makeALongRequest("some","params",function(result,data,xhr){
alert("this is the request callback");
});
//Always return false in the onsubmit event if you want to stop the browser from trying to POST anything
return false;
}
…字段。。。
函数getCoords(){
geocoder.makeALongRequest(“一些”、“参数”、函数(结果、数据、xhr){
警报(“这是请求回调”);
});
//如果要阻止浏览器尝试发布任何内容,请在onsubmit事件中始终返回false
返回false;
}
很显然,我不该去洗手间。哦,好吧。
<form onsubmit="getCoords()">
...fields...
</form>
<script>
function getCoords(){
geocoder.makeALongRequest("some","params",function(result,data,xhr){
alert("this is the request callback");
});
//Always return false in the onsubmit event if you want to stop the browser from trying to POST anything
return false;
}