使用单选按钮onclick和javascript函数动态填充数字字段?

使用单选按钮onclick和javascript函数动态填充数字字段?,javascript,php,gravity-forms-plugin,Javascript,Php,Gravity Forms Plugin,我有三件事:1。两个数字字段(来自标准类),我希望动态填充它们。2.一个Javascript函数,用于使用地理定位器提取坐标;3.单击单选按钮时,数字字段将动态填充 下面是我用来完成任务的代码,但它不起作用。你能帮我一下吗?我做错了什么?提前感谢您的帮助 **我已检查我的地理定位器功能是否正常工作。我想,我的问题在于代码,它提取代码并动态填充数字字段 add_filter('gform_field_value_longitude', 'populate_longitude'); function

我有三件事:1。两个数字字段(来自标准类),我希望动态填充它们。2.一个Javascript函数,用于使用地理定位器提取坐标;3.单击单选按钮时,数字字段将动态填充

下面是我用来完成任务的代码,但它不起作用。你能帮我一下吗?我做错了什么?提前感谢您的帮助

**我已检查我的地理定位器功能是否正常工作。我想,我的问题在于代码,它提取代码并动态填充数字字段

add_filter('gform_field_value_longitude', 'populate_longitude');
function populate_longitude($value){
?>
<script type="text/javascript">
    $(document).ready(function() {
        var x=document.getElementById("latitude");
        var y=document.getElementById("longitude");

    if(navigator.geolocation){
    navigator.geolocation.getCurrentPosition(showLocation, errorHandler, {enableHighAccuracy:false, maximumAge:60000, timeout:27000});
    }else{
        alert('Votre navigateur ne prend malheureusement pas en charge la géolocalisation.');
        }
    });

    function showLocation(position){
        x.value=position.coords.latitude;  
        y.value=position.coords.longitude;
    }

    <input type="radio" value="extract_coordinate" onclick="populate_longitude;">

    </script>
<?php
    return "$y";
}
add_filter('gform_字段_值_经度','populate_经度');
函数填充经度($value){
?>
$(文档).ready(函数(){
var x=document.getElementById(“纬度”);
var y=document.getElementById(“经度”);
if(导航器.地理位置){
getCurrentPosition(showLocation,errorHandler,{EnableHighAccurance:false,maximumAge:60000,timeout:27000});
}否则{
警报(‘导航者在全球定位中的收费’);
}
});
功能显示位置(位置){
x、 值=位置坐标纬度;
y、 值=position.coords.longitude;
}

这段代码有效吗?就像在浏览器中打开页面时一样,它是否显示了所有必需的字段?我不知道您正在使用的插件,但代码可能有语法错误

不过,引起我注意的第一件事是,在onclick事件中,您实际上并没有调用该函数。请尝试以下操作:

<input type="radio" value="extract_coordinate" onclick="populate_longitude();">

您在这方面有什么进展吗?如果您需要修复OP中的代码,而不是您提供的链接中的代码,请检查以下内容。您的函数应该位于标记内,因为您需要从onclick事件调用它,所以它应该是Javascript。您还需要删除document.ready,只需使用常规函数即可

//add_filter('gform_field_value_longitude', 'populate_longitude');
<script type="text/javascript">
    function populate_longitude(){
        var x=document.getElementById("latitude");
        var y=document.getElementById("longitude");

        if(navigator.geolocation){
           navigator.geolocation.getCurrentPosition(showLocation);
        }else{
           alert('Votre navigateur ne prend malheureusement pas en charge la géolocalisation.');
        }
     }

     function showLocation(position){
         document.getElementById("latitude").value = position.coords.latitude;
         document.getElementById("longitude").value =position.coords.longitude;
     }
</script>

<input type="radio" value="extract_coordinate" onclick="populate_longitude();">
<input type="text" value="" id="latitude">
<input type="text" value="" id="longitude">
//添加_过滤器('gform_字段_值_经度','populate_经度');
函数填充经度(){
var x=document.getElementById(“纬度”);
var y=document.getElementById(“经度”);
if(导航器.地理位置){
navigator.geolocation.getCurrentPosition(showLocation);
}否则{
警报(‘导航者在全球定位中的收费’);
}
}
功能显示位置(位置){
document.getElementById(“纬度”).value=position.coords.latitude;
document.getElementById(“经度”).value=position.coords.longitude;
}

您好。我在上面提供的代码,我已经在wordpress的function.php页面中输入了这段代码。是的,代码正在工作。我正在尝试使用w3学校网站上的html5地理定位功能。是的,页面显示了所有字段要求,这里是页面链接,您可以在其中找到重力表单:。关于syntax错误,对不起,那是我的错误。我已经更正了,但仍然不起作用。有什么线索吗?如果您需要更多关于这方面的信息,请告诉我。谢谢。我已经得到了它,但在您链接的页面的源代码中做了一些更改。1)第一个无线电标签应该在结束标签之外(第133-135行).2)在函数“updateFields”中,“document.getElementById(“mylon”).value”指向不存在的ID,因此没有实际字段被更新。3)在“getCurrentPosition”中传递的函数“locationError”不存在。4)实际显示的单选按钮在单击时不起任何作用。4)将所有函数都包含在文档中。ready块无法实现单选按钮单击的目的。