【问题标题】:multiple buttons posting to self through a javascript多个按钮通过 javascript 发布给自己
【发布时间】:2018-06-18 16:04:52
【问题描述】:

我正在尝试设置一个表单来发布给自己,但也在过程中间做一些 javascript...我有这个工作,但下一步是能够识别一个数字表单上被按下的按钮数。

<button type="button" name="used" id="used" class="btn btn-primary" onclick="getNewLocation(used)">Used Hole</button>
<button type="button" name="disused" class="btn btn-primary" onclick="getNewLocation(used)">Disused Hole</button>

这里的示例是 2 个按钮,但有 4 个。 getNewLocation 然后在提交回页面之前执行以下操作以刷新位置。

function getNewLocation(x) {
                if (navigator.geolocation) {
                    navigator.geolocation.getCurrentPosition(sendPosition,showError,{enableHighAccuracy: true});
                    var elem = document.getElementById("button");
                    elem.value = x;

                } else { 
                    x = "Geolocation is not supported by this browser.";
                }
            }

            function sendPosition(position) {
                document.getElementById("lat").value = position.coords.latitude;
                document.getElementById("long").value = position.coords.longitude;
                document.getElementById("acc").value = position.coords.accuracy;
                document.forms["point"].submit();
            }

这实际上有效,但不通过按钮值,而是将 [button] 的 $_POST 值作为 object HTMLButtonElement :-(

谁能指点我正确的方向??

【问题讨论】:

  • 不确定,但&lt;input type="button"&gt; 而不是button 可能有用..?不过,通常这种数据是在隐藏的输入中传递的。
  • “按钮值”是什么意思?你如何为按钮设置任何值?你使用getElementById("button") - 那是什么?!然后你给它设置了一些“值”,然后认为只是一个按钮的一个属性是通过一个表单发送的?为此使用输入字段!

标签: javascript forms element


【解决方案1】:

您遇到的主要问题是在使用var elem = document.getElementById("button"); 访问按钮后尝试设置按钮的值您会注意到,您的两个按钮都没有按钮的id 属性,因此这不起作用。

将按钮本身作为第一个参数传递给getNewLocation 可能会有所帮助,允许您直接设置它的value

您的代码还有其他问题,但就正确的方向而言,我希望这会有所帮助。

function getNewLocation(elem) {
  if (navigator.geolocation) { 
    // var elem = document.getElementById("button");
    elem.value = 'used';
  }
}
<button type="button" name="used" id="used" class="btn btn-primary" onclick="getNewLocation(this)">Used Hole</button>
<button type="button" name="disused" class="btn btn-primary" onclick="getNewLocation(this)">Disused Hole</button>

【讨论】:

  • 他正在传递一些“已使用”属性并将元素(按钮)的值设置为该属性。或更新属性。因此,您的回答只是对正在发生的事情的建议,而不是明确的建议。即使带有按钮的部分是真实的并且我已经写过它:)
  • 我不确定以上是否有意义...我有 2 个按钮,需要将 $_POST 变量 [button] 的属性元素设置为按钮的名称,以便我可以然后直接基于 $_POST["button"] 变量的代码。
  • 因此,如果其中一个按钮发送 getNewLocation(used),我将需要 $_POST["button"] = 'used'...。但是如果按下另一个按钮 onclick="然后 getNewLocation(disused) 将刷新位置,但 $_POST["button"] 将等于 'disused'
  • 我的错....我应该说我还尝试在表单上放置一个隐藏的输入,名称和 id 为“按钮”,并尝试设置它的值,在提交之前使用脚本无济于事
【解决方案2】:

好的,感谢您的宝贵意见...花了一些时间和更多的尝试,但我最终到达了那里....

<input type="hidden" name="button" id="button">
<button type="button" name="used" id="used" class="btn btn-primary" onclick="getNewLocation('used')">Used Hole</button>
<button type="button" name="disused" id="disused" class="btn btn-primary" onclick="getNewLocation('disused')">Disused Hole</button>

&lt;script&gt;中有这个

    function getNewLocation(x) {
                if (navigator.geolocation) {
                    var elem = document.getElementById("button");
                    elem.value = x;
                    navigator.geolocation.getCurrentPosition(sendPosition,showError,{enableHighAccuracy: true});

                } else { 
                    x = "Geolocation is not supported by this browser.";
                }
            }

            function sendPosition(position) {
                document.getElementById("lat").value = position.coords.latitude;
                document.getElementById("long").value = position.coords.longitude;
                document.getElementById("acc").value = position.coords.accuracy;
                document.forms["point"].submit();
            }

再次感谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-14
    • 2020-05-29
    • 2011-10-29
    • 2020-09-02
    • 1970-01-01
    • 1970-01-01
    • 2017-01-07
    相关资源
    最近更新 更多