使用 jquery 访问表单中的值时出现问题

发布于 2024-12-26 11:34:47 字数 2333 浏览 5 评论 0原文

我正在编写一个工具,它接受用户输入的地址并将其转换为 GoogleMaps api 的地理位置。

编辑:不过,我确实想提交表单数据..我想。我不确定是否需要提交表单,但最终结果是它应该在 wordpress 中生成一个帖子,其中包含 titleaddressgeo值。

如果删除

标记,则提交按钮调用的方法可以正常工作。如果

标签位于表单周围,则仍会调用该方法,但不会对任何内容进行地理编码。我的表格设置不正确吗?

这是形式:

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

執念 2025-01-02 11:34:47

将输入按钮从 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.

深居我梦 2025-01-02 11:34:47

使用元素时,最好将此地理编码函数绑定到 onsubmit 事件 为此。

<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;
}

When the elements are being used, it's a good idea to bind this geocode function to the onsubmit event for that .

<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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文