getElementById is not being updated

时间:2016-02-12 21:08:13

标签: javascript jquery html5

My JS :

for (var propertyName in data) { console.log(data[propertyName]; }

My HTML :

function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position){
            document.getElementById("xc").value = position.coords.latitude;
            document.getElementById("yc").value = position.coords.longitude;                                                    
            document.getElementById("loginform").submit;
        });     
    } else {
        document.getElementById("xc").value = '0';
        document.getElementById("yc").value = '0';
        document.getElementById("loginform").submit;
    }
}

I've tried everything, from an <form id="loginform" action="./functions/dologin.php" method="post"> <input type="hidden" id="xc" name="xc" value="X"> <input type="hidden" id="yc" name="yc" value="X"> <input class="myButton" type="submit" onclick="getLocation();" value="Login"> </form> in the form - with button type submit, to jQuery to DOM

The value is always "X".

5 个答案:

答案 0 :(得分:2)

Your form is getting submitted which is just causing the page to get reloaded which makes the changes you just made to the page get overwritten by the newly loaded page.

So, this is what it does:

  1. Click button
  2. Your Javascript code runs that modifies the page
  3. Form gets submittted
  4. Page gets reloaded - all your changes you just make to the page disappear

If you add a SELECT TE2Clndr.BUS_DT as Cal_Dt , Max(TE2Clndr_1.BUS_DT) AS Next_Bus_Dt FROM TE2Clndr INNER JOIN TE2Clndr AS TE2Clndr_1 ON TE2Clndr.PRIR_BUS_DT = TE2Clndr_1.PRIR_BUS_DT GROUP BY TE2Clndr.BUS_DT; to the end of your return false; and then return that in the HTML, it should stop the form from getting submitted.

getLocation()

You could also change your button to just be a regular button, not a submit button and then there would be no default submit of the form.

function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position){
            document.getElementById("xc").value = position.coords.latitude;
            document.getElementById("yc").value = position.coords.longitude;                                                    
            document.getElementById("loginform").submit;
        });     
    } else {
        document.getElementById("xc").value = '0';
        document.getElementById("yc").value = '0';
        document.getElementById("loginform").submit;
    }
    // prevent default submit of the form
    return false;
}

<form id="loginform" action="./functions/dologin.php" method="post">
    <input type="hidden" id="xc" name="xc" value="X">
    <input type="hidden" id="yc" name="yc" value="X">
    <input class="myButton" type="submit" onclick="return getLocation();" value="Login">               
</form>

答案 1 :(得分:1)

Thanks everybody! I made a couple of mistakes, and you all added! Code that works:

width

答案 2 :(得分:0)

You call the function when the user clicks the submit button.

The function runs. Then the form submits.

The browser loads the new page before getting a response to the asynchronous location request.

You need to class Number<T> { var val: T? } protocol Merge { func merge(from: Self, into: Self) } extension Number: Merge { func merge(from: Number, into: Number) { into.val = from.val } } from your onclick function:

return false

Better yet, use a modern way to bind events and call onclick="getLocation(); return false;" .

答案 3 :(得分:0)

change the type of the input to title <- paste("An analysis of the mtcars dataset ") subheader <- paste("How does mpg change by \nnumber of cyl?") library(gridExtra) library(grid) table_label <- function(label, params=list()) { params1 <- modifyList(list(hjust=0, x=0, fontsize=12, fontface=2), params) params2 <- modifyList(list(hjust=0, x=0, fontsize=8, fontface=1), params) mytheme <- ttheme_minimal(core = list(fg_params = params2), colhead = list(fg_params = params1)) disect <- strsplit(label, "\\n")[[1]] m <- as.matrix(disect[-1]) g <- tableGrob(m, cols=disect[1], theme=mytheme) g$widths <- unit(1,"npc") g } p <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) + geom_line() ## add a title, note: centering is defined wrt the whole plot grid.arrange(p, top=table_label(paste0(title,"\n",subheader), params=list(x=0.1))) ## to align precisely with the panel, use gtable instead library(gtable) titleify <- function(p, label, ...){ g <- ggplotGrob(p) title <- table_label(label, ...) g <- gtable_add_grob(g, title, t=1, l=4) g$heights <- grid:::unit.list(g$heights) g$heights[1] <-list(sum(title$heights)) grid.newpage() grid.draw(g) } titleify(p, paste0(title,"\n",subheader)) ## we can also hack ggplot2 to define a custom element for the title ## though be warned the hardware supporting your computer may be damaged by head banging element_custom <- function() { structure(list(), class = c("element_custom", "element_text")) } element_grob.element_custom <- function(element, label="", ...) { table_label(label) } # default method is unreliable heightDetails.gtable <- function(x) sum(x$heights) ggplot(iris, aes(Sepal.Length, Sepal.Width)) + geom_line() + ggtitle(paste0(title,"\n",subheader))+ (theme_grey() %+replace% theme(plot.title = element_custom())) instead of type="button" see fiddle: https://jsfiddle.net/16mgy0xm/11/

type="submit"

答案 4 :(得分:0)

事实证明,Chrome在非安全网站(如我的开发环境)中折旧位置