使用 jquery 访问表单中的值时出现问题
我正在编写一个工具,它接受用户输入的地址并将其转换为 GoogleMaps api 的地理位置。
编辑:不过,我确实想提交表单数据..我想。我不确定是否需要提交表单,但最终结果是它应该在 wordpress 中生成一个帖子,其中包含 title
、address
和 geo值。
如果删除 标记,则提交按钮调用的方法可以正常工作。如果
这是形式:
$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()?>
这是相关函数:
function getCoordinates() {
//variables are set up on page initialization
geocoder.geocode( { 'address': $('#address').val() }, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
alert ('latitude: ' + results[0].geometry.location.lat() + ' longitude: ' + results[0].geometry.location.lng());
$('#latitude').val( results[0].geometry.location.lat() ) ;
$('#longitude').val( results[0].geometry.location.lng() ) ;
$geo = results[0].geometry.location.lat() + "," + results[0].geometry.location.lng();
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
}
});
}
I'm writing a tool that takes a user-inputted address and converts it to a geolocation for GoogleMaps' api.
Edit: I do want to submit the form data, though.. I think. I'm not sure if submitting the form is necessary, but the end result is that it should generate a post in wordpress containing the title
, address
, and geo
values.
The method called by the submit button works correctly if the <form></form>
tags are removed. If the <form>
tags are around the form, the method will still be called but it won't geocode anything. Did I set up my form incorrectly?
Here is the form:
$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()?>
Here is the relevant function:
function getCoordinates() {
//variables are set up on page initialization
geocoder.geocode( { 'address': $('#address').val() }, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
alert ('latitude: ' + results[0].geometry.location.lat() + ' longitude: ' + results[0].geometry.location.lng());
$('#latitude').val( results[0].geometry.location.lat() ) ;
$('#longitude').val( results[0].geometry.location.lng() ) ;
$geo = results[0].geometry.location.lat() + "," + results[0].geometry.location.lng();
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
}
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将输入按钮从 type="submit" 更改为 type="button" 并关闭表单标签。仅当您想要实际提交表单时,才使用内部带有提交按钮的表单,而不是执行仅 javascript 操作。
Change the input button from type="submit" to type="button" and leave the form tags off. You only use a form with a submit button inside when you want to actually submit the form, not perform a javascript only action.
使用元素时,最好将此地理编码函数绑定到 onsubmit 事件 为此。
When the elements are being used, it's a good idea to bind this geocode function to the onsubmit event for that .