使用jQuery获取用户坐标

时间:2016-05-07 15:40:38

标签: javascript jquery geolocation

需要你的帮助。

我不确定我在做什么,而且我还将jQuery和JS混合在一起,只是没有任何意义,但我不知道该怎么做以及如何处理这个问题。 / p>

我希望value#third-input的{​​{1}}使用form form后的坐标进行更新。

submitted

这是我的jQuery for this form

<form class="flex-form" action="MAILTO:someone@example.com" method="post" enctype="text/plain">
<input id="first-input" type="text" placeholder="What is your name?" onfocus="this.placeholder = ''" onblur="this.placeholder = 'What is your name?'" value="" name="name">
<br />
<input id="second-input" type="text" pattern="[0-9]*" placeholder="Enter your phone number" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Enter your phone number'" value="" name="phone-number">
<br />
<input id="third-input" type="text" placeholder="Press button and Share location" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Press button and Share location'" value="" disabled="disabled" name="coordinates">
<br />
<input type="submit" value="Send Us Request">
</form>

我从此链接[http://www.w3schools.com/][1]

中获取的地理位置提示

正如你所看到的,我对它很陌生,但我可以向你保证,我做了一个健康的搜索并没有找到任何解决方案。我也想避免使用插件。

感谢。

欢迎使用评论代码,我只想确定&#34;什么做什么&#34;。

[1]:w3schools.com上的地理位置

1 个答案:

答案 0 :(得分:1)

  1. 您的javascript代码似乎被删除或遗漏了某些{}
  2. 我可以建议将required attribute添加到输入字段,而不是手动检查吗?然后,您可以使用:invalid:valid伪类对其进行样式设置。
  3. .val()是一个吸气剂,一旦你拥有了自己的输入值字符串。你想要做的是使用setter方法.val("geo not supported")
    • 您永远不能将某个功能指定为其他功能。 (val() = ''
    • .val();删除$('#third-input').val();就足够了
    • 但我不认为您应该将该值设置为不受支持的说明,因为该字段应仅包含位于上方或其他位置的位置
  4. 对于电话号码字段,我会使用tel类型而不是text
  5. <br>已经自动关闭,因此无需添加/
  6. IMO placeholder不适用于标记输入字段。它们应该用作示例输入(阅读this
  7. 投掷一些autocompletion也不会那么糟糕
  8. 如果您在关注输入字段时不喜欢占位符,请使用css :focus + ::placeholder将其删除,而不是在blur / focus上设置/删除占位符属性
    • IMO我认为它应该是静态的,让它一个人待着
  9. 以下是我的解决方案:https://jsfiddle.net/1u8v0mam/1/