0

当我单击signin.xhtml上的 commandButton 时,我想运行一个 javascript 函数,但是当我单击它时,会重定向到dev.xhtml。我不想要这种重定向,我想留在signin.xhtml上。实际上它并不是真正的重定向,因为它是同一个index.xhtml页面,但具有延迟加载。

这是我来自signin.xhtml的 commandButton :

<h:form id="formSignInPartner" enctype="multipart/form-data">
  <p:commandButton value="Géolocaliser mon commerce" onclick="getLocation()"/> 
</h:form>

index.xhtml

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:pm="http://primefaces.org/mobile"
      xmlns:f="http://xmlns.jcp.org/jsf/core"
      xmlns:p="http://primefaces.org/ui">
     <f:view renderKitId="PRIMEFACES_MOBILE" />
     <h:head>
        <script type="text/javascript" src="../javascript/geolocalisationPartner.js"></script>
     </h:head>
     <h:body>
        <pm:page id="dev" lazy ="false">
            <ui:include src="trash/dev.xhtml"/>
        </pm:page>
        <pm:page id="partnerSignIn" lazy="true">
            <ui:include src="/Partner/signIn.xhtml"/>
        </pm:page>
      </h:body>
    </html>

这里是脚本geolocalisationPartner.js但我不认为这是问题所在

var options = {
    enableHighAccuracy: true,
    timeout: 5000,
    maximumAge: 0
};
function error(msg) {
    alert('Geolocalisation échouée :\n' + msg);
}
function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition, error, options);
    } else {
        alert('Votre navigateur\nne supporte pas\nla geolocalisation');
     }
}

function showPosition(position) {
    var long = position.coords.longitude;
    var lat = position.coords.latitude;
    $(PrimeFaces.escapeClientId('partnerSignIn:formSignInPartner:idlongitude')).val(long);
    $(PrimeFaces.escapeClientId('partnerSignIn:formSignInPartner:idlatitude')).val(lat);
    alert('Geolocalisation réussie !');

}
4

3 回答 3

0

您可以使用event.preventDefault

脚本

function getLocation(e) {
    e.preventDefault()
    // your code here
}

xhtml

<p:commandButton value="Géolocaliser mon commerce" onclick="getLocation(e)"/>
于 2015-05-19T09:50:28.310 回答
0

如果您不希望它链接,则 需要取消该事件。

function getLocation(evt) {
     evt.preventDefault();
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition, error, options);
    } else {
        alert('Votre navigateur\nne supporte pas\nla geolocalisation');
     }
}

更新您的 html:

<p:commandButton value="Géolocaliser mon commerce" onclick="getLocation(evt)"/> 
于 2015-05-19T09:50:34.780 回答
0

我发现了问题。在 javascrip 控制台中,我看到我的脚本文件没有找到,路径错误。

于 2015-05-22T19:44:26.687 回答