Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [JavaScript]wynik z js w polu input
Forum PHP.pl > Forum > Przedszkole
maksik
Witam
mam taki javascript
  1. <script>$("#miasto").html(geoip_city());</script>


jednakże nie wyświetla mi się on w polu input, a tylko w zwyklym divie. Co z nim jest nie poprawnie?
  1. <input type="text" id="miasto" name="miasto">
nospor
1) A nie masz przypadkiem div o ID miasto?
2) Nie HTML a value. Pola input nie mają HTML...
maksik
ta funkcja wczesniej wyświetlała miasto z ip w postaci <strong id="miasto"></strong> ale również wyświetla się w divie
a chciałbym ją przenieść do input text
nospor
Napisalem dwa proste punkty. Umiesz na nie odpowiedziec?
maksik
nie ma div, a zmiana html na value nie pomaga
nospor
Pokaz caly kod po poprawkach, caly HTML, pokaz tez jak wyglada funkcja geoip_city()
maksik
  1. http://j.maxmind.com/app/geoip.js


oraz

  1. var MeOnTheMap = function(options){
  2.  
  3. this.options = {
  4. address : "",
  5. container : "",
  6. defaultUI : true,
  7. noDragging : false,
  8. html : "",
  9. zoomLevel : 16,
  10. view : 0
  11. };
  12.  
  13. this.preloads = [];
  14.  
  15. this.initialize = function(options) {
  16.  
  17. for (var opt in options){
  18. this.options[opt] = options[opt];
  19. }
  20.  
  21. this.preparePreloading();
  22.  
  23. this.container = document.getElementById(this.options.container);
  24. if (!this.container) {
  25. alert("Could not locate \"" + this.options.container + "\"");
  26. return;
  27. }
  28.  
  29. this.findLocation();
  30. };
  31.  
  32. this.preparePreloading = function(){
  33. var regxp = new RegExp('(src)=("[^"]*")','g');
  34. var sources = this.options.html.match(regxp);
  35.  
  36. if (!sources)
  37. return;
  38.  
  39. function getHandler(obj) {
  40. return function(){
  41. var el = document.getElementById(obj.id);
  42. if (el){
  43. el.parentNode.replaceChild(this, el);
  44. obj.marker.tooltip.redraw(true);
  45. }
  46. };
  47. };
  48.  
  49. for (var i = 0; i < sources.length; i++){
  50. this.options.html = this.options.html.replace(sources[i],"style=\"visibility:visible\" id=\"preloadimg" + i + "\" src=\"\"");
  51.  
  52. var src = sources[0].split("=\"")[1];
  53. src = src.substring(0,src.length - 1);
  54.  
  55. var img = new Image();
  56.  
  57. this.preloads.push({
  58. element: img,
  59. src: src,
  60. id: "preloadimg" + i
  61. });
  62.  
  63. img.onload = getHandler(this.preloads[this.preloads.length - 1]);
  64. }
  65. };
  66.  
  67. this.startPreloading = function(marker, map){
  68. for (var i = 0; i < this.preloads.length; i++) {
  69. this.preloads[i].marker = marker;
  70. this.preloads[i].map = map;
  71. this.preloads[i].element.src = this.preloads[i].src;
  72. }
  73. };
  74.  
  75. this.findLocation = function() {
  76. var _this = this;
  77. this.geoLocator = new GClientGeocoder();
  78. this.geoLocator.getLatLng(this.options.address, function(response){
  79. _this.handleGetLocatorResponse(response);
  80. });
  81. };
  82.  
  83. this.getIcon = function() {
  84. var icon = new GIcon(G_DEFAULT_ICON);
  85. icon.image = "images/icon.png";
  86. icon.shadow = "images/shadow.png";
  87. icon.iconSize = new GSize(35,35);
  88. icon.shadowSize = new GSize(52, 35);
  89. icon.iconAnchor = new GPoint(17, 35);
  90. return icon;
  91. };
  92.  
  93. this.handleGetLocatorResponse = function(response){
  94. this.geoLocator = null;
  95.  
  96. if(!response) {
  97. alert("Could not resolve this addess \"" + this.options.address + "\"");
  98. return;
  99. }
  100.  
  101. var type = "";
  102. switch (this.options.view) {
  103. case 1 : type = G_SATELLITE_MAP; break;
  104. case 2 : type = G_HYBRID_MAP; break;
  105. default : type = G_NORMAL_MAP; break;
  106. }
  107.  
  108. this.map = new GMap2(this.container);
  109. this.map.setMapType(type);
  110. this.map.setCenter(response, this.options.zoomLevel);
  111. this.map.disableScrollWheelZoom();
  112.  
  113. if (this.options.noDragging)
  114. this.map.disableDragging();
  115.  
  116. if (this.options.defaultUI)
  117. this.map.setUIToDefault();
  118.  
  119. this.marker = new GMarker(response,{icon: this.getIcon()});
  120. this.map.addOverlay(this.marker);
  121.  
  122. if (!this.options.html || this.options.html == "")
  123. return;
  124.  
  125. this.marker.tooltip = new Tooltip(this.marker, this.options.html);
  126. this.map.addOverlay(this.marker.tooltip);
  127. this.marker.tooltip.show();
  128.  
  129. if (this.newCenter) {
  130. var markerPos = this.map.fromLatLngToDivPixel(this.marker.getPoint());
  131. var pos = this.map.fromContainerPixelToLatLng({
  132. x: markerPos.x + this.newCenter.x,
  133. y: markerPos.y + this.newCenter.y
  134. });
  135. this.map.setCenter(pos);
  136. }
  137.  
  138. this.startPreloading(this.marker, this.map);
  139. };
  140.  
  141. this.adjustMapCenter = function(position){
  142. if (!this.geoLocator && this.map){
  143. var markerPos = this.map.fromLatLngToDivPixel(this.marker.getPoint());
  144. var pos = this.map.fromContainerPixelToLatLng({
  145. x: markerPos.x + position.x,
  146. y: markerPos.y + position.y
  147. });
  148. this.map.setCenter(pos);
  149. } else {
  150. this.newCenter = position;
  151. }
  152. };
  153.  
  154. this.initialize(options);
  155. };
  156.  
  157. function Tooltip(marker, text){
  158.  
  159. this.isIE6 = function(){
  160. if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){
  161. var ieversion = new Number(RegExp.$1);
  162. return (ieversion == 6);
  163. }
  164. return false;
  165. };
  166.  
  167. this.initialize = function(map){
  168. this.map = map;
  169. this.div = document.createElement("div");
  170. var top = document.createElement("div"); top .className = "top" + ((this.isIE6()) ? " IE6" : "");
  171. var middle = document.createElement("div"); middle.className = "middle" + ((this.isIE6()) ? " MIDDLEIE6" : "");
  172. var bottom = document.createElement("div"); bottom.className = "bottom" + ((this.isIE6()) ? " BOTTOMIE6" : "");
  173.  
  174. middle.innerHTML = text;
  175.  
  176. this.div.appendChild(top );
  177. this.div.appendChild(middle);
  178. this.div.appendChild(bottom);
  179.  
  180. this.div.className = 'tooltip';
  181. this.div.style.position = 'absolute';
  182. this.div.style.visibility = 'hidden';
  183.  
  184. map.getPane(G_MAP_FLOAT_PANE).appendChild(this.div);
  185. };
  186.  
  187. this.remove = function(){
  188. this.div.parentNode.removeChild(this.div);
  189. };
  190.  
  191. this.copy = function(){
  192. return new Tooltip(this.marker, this.text, this.padding);
  193. };
  194.  
  195. this.redraw = function(force){
  196. if (!force)
  197. return;
  198.  
  199. var markerPos = this.map.fromLatLngToDivPixel(this.marker.getPoint());
  200. var iconAnchor = this.marker.getIcon().iconAnchor;
  201. var xPos = (markerPos.x - (this.div.offsetWidth / 2));
  202. var yPos = markerPos.y - iconAnchor.y - this.div.offsetHeight;
  203. this.div.style.top = yPos + 'px';
  204. this.div.style.left = xPos + 'px';
  205. };
  206.  
  207. this.show = function(){
  208. this.div.style.visibility = 'visible';
  209. };
  210.  
  211. this.hide = function(){
  212. this.div.style.visibility = 'hidden';
  213. };
  214.  
  215. this.marker = marker;
  216. this.text = text;
  217. this.prototype = new GOverlay();
  218. };
nospor
Prosilem o kod HTML. W kodzie co pokazales nigdzie nie widze inputa o ID MIASTO

zas do linka co podales nie mam dostepu. 500 SERVER ERROR
maksik
  1. <script src="http://j.maxmind.com/app/geoip.js"></script>
  2. <script type="text/javascript">
  3.  
  4. var MeOnTheMap = function(options){
  5.  
  6. this.options = {
  7. address : "",
  8. container : "",
  9. defaultUI : true,
  10. noDragging : false,
  11. html : "",
  12. zoomLevel : 16,
  13. view : 0
  14. };
  15.  
  16. this.preloads = [];
  17.  
  18. this.initialize = function(options) {
  19.  
  20. for (var opt in options){
  21. this.options[opt] = options[opt];
  22. }
  23.  
  24. this.preparePreloading();
  25.  
  26. this.container = document.getElementById(this.options.container);
  27. if (!this.container) {
  28. alert("Could not locate \"" + this.options.container + "\"");
  29. return;
  30. }
  31.  
  32. this.findLocation();
  33. };
  34.  
  35. this.preparePreloading = function(){
  36. var regxp = new RegExp('(src)=("[^"]*")','g');
  37. var sources = this.options.html.match(regxp);
  38.  
  39. if (!sources)
  40. return;
  41.  
  42. function getHandler(obj) {
  43. return function(){
  44. var el = document.getElementById(obj.id);
  45. if (el){
  46. el.parentNode.replaceChild(this, el);
  47. obj.marker.tooltip.redraw(true);
  48. }
  49. };
  50. };
  51.  
  52. for (var i = 0; i < sources.length; i++){
  53. this.options.html = this.options.html.replace(sources[i],"style=\"visibility:visible\" id=\"preloadimg" + i + "\" src=\"\"");
  54.  
  55. var src = sources[0].split("=\"")[1];
  56. src = src.substring(0,src.length - 1);
  57.  
  58. var img = new Image();
  59.  
  60. this.preloads.push({
  61. element: img,
  62. src: src,
  63. id: "preloadimg" + i
  64. });
  65.  
  66. img.onload = getHandler(this.preloads[this.preloads.length - 1]);
  67. }
  68. };
  69.  
  70. this.startPreloading = function(marker, map){
  71. for (var i = 0; i < this.preloads.length; i++) {
  72. this.preloads[i].marker = marker;
  73. this.preloads[i].map = map;
  74. this.preloads[i].element.src = this.preloads[i].src;
  75. }
  76. };
  77.  
  78. this.findLocation = function() {
  79. var _this = this;
  80. this.geoLocator = new GClientGeocoder();
  81. this.geoLocator.getLatLng(this.options.address, function(response){
  82. _this.handleGetLocatorResponse(response);
  83. });
  84. };
  85.  
  86. this.getIcon = function() {
  87. var icon = new GIcon(G_DEFAULT_ICON);
  88. icon.image = "images/icon.png";
  89. icon.shadow = "images/shadow.png";
  90. icon.iconSize = new GSize(35,35);
  91. icon.shadowSize = new GSize(52, 35);
  92. icon.iconAnchor = new GPoint(17, 35);
  93. return icon;
  94. };
  95.  
  96. this.handleGetLocatorResponse = function(response){
  97. this.geoLocator = null;
  98.  
  99. if(!response) {
  100. alert("Could not resolve this addess \"" + this.options.address + "\"");
  101. return;
  102. }
  103.  
  104. var type = "";
  105. switch (this.options.view) {
  106. case 1 : type = G_SATELLITE_MAP; break;
  107. case 2 : type = G_HYBRID_MAP; break;
  108. default : type = G_NORMAL_MAP; break;
  109. }
  110.  
  111. this.map = new GMap2(this.container);
  112. this.map.setMapType(type);
  113. this.map.setCenter(response, this.options.zoomLevel);
  114. this.map.disableScrollWheelZoom();
  115.  
  116. if (this.options.noDragging)
  117. this.map.disableDragging();
  118.  
  119. if (this.options.defaultUI)
  120. this.map.setUIToDefault();
  121.  
  122. this.marker = new GMarker(response,{icon: this.getIcon()});
  123. this.map.addOverlay(this.marker);
  124.  
  125. if (!this.options.html || this.options.html == "")
  126. return;
  127.  
  128. this.marker.tooltip = new Tooltip(this.marker, this.options.html);
  129. this.map.addOverlay(this.marker.tooltip);
  130. this.marker.tooltip.show();
  131.  
  132. if (this.newCenter) {
  133. var markerPos = this.map.fromLatLngToDivPixel(this.marker.getPoint());
  134. var pos = this.map.fromContainerPixelToLatLng({
  135. x: markerPos.x + this.newCenter.x,
  136. y: markerPos.y + this.newCenter.y
  137. });
  138. this.map.setCenter(pos);
  139. }
  140.  
  141. this.startPreloading(this.marker, this.map);
  142. };
  143.  
  144. this.adjustMapCenter = function(position){
  145. if (!this.geoLocator && this.map){
  146. var markerPos = this.map.fromLatLngToDivPixel(this.marker.getPoint());
  147. var pos = this.map.fromContainerPixelToLatLng({
  148. x: markerPos.x + position.x,
  149. y: markerPos.y + position.y
  150. });
  151. this.map.setCenter(pos);
  152. } else {
  153. this.newCenter = position;
  154. }
  155. };
  156.  
  157. this.initialize(options);
  158. };
  159.  
  160. function Tooltip(marker, text){
  161.  
  162. this.isIE6 = function(){
  163. if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){
  164. var ieversion = new Number(RegExp.$1);
  165. return (ieversion == 6);
  166. }
  167. return false;
  168. };
  169.  
  170. this.initialize = function(map){
  171. this.map = map;
  172. this.div = document.createElement("div");
  173. var top = document.createElement("div"); top .className = "top" + ((this.isIE6()) ? " IE6" : "");
  174. var middle = document.createElement("div"); middle.className = "middle" + ((this.isIE6()) ? " MIDDLEIE6" : "");
  175. var bottom = document.createElement("div"); bottom.className = "bottom" + ((this.isIE6()) ? " BOTTOMIE6" : "");
  176.  
  177. middle.innerHTML = text;
  178.  
  179. this.div.appendChild(top );
  180. this.div.appendChild(middle);
  181. this.div.appendChild(bottom);
  182.  
  183. this.div.className = 'tooltip';
  184. this.div.style.position = 'absolute';
  185. this.div.style.visibility = 'hidden';
  186.  
  187. map.getPane(G_MAP_FLOAT_PANE).appendChild(this.div);
  188. };
  189.  
  190. this.remove = function(){
  191. this.div.parentNode.removeChild(this.div);
  192. };
  193.  
  194. this.copy = function(){
  195. return new Tooltip(this.marker, this.text, this.padding);
  196. };
  197.  
  198. this.redraw = function(force){
  199. if (!force)
  200. return;
  201.  
  202. var markerPos = this.map.fromLatLngToDivPixel(this.marker.getPoint());
  203. var iconAnchor = this.marker.getIcon().iconAnchor;
  204. var xPos = (markerPos.x - (this.div.offsetWidth / 2));
  205. var yPos = markerPos.y - iconAnchor.y - this.div.offsetHeight;
  206. this.div.style.top = yPos + 'px';
  207. this.div.style.left = xPos + 'px';
  208. };
  209.  
  210. this.show = function(){
  211. this.div.style.visibility = 'visible';
  212. };
  213.  
  214. this.hide = function(){
  215. this.div.style.visibility = 'hidden';
  216. };
  217.  
  218. this.marker = marker;
  219. this.text = text;
  220. this.prototype = new GOverlay();
  221. };
  222. </script>
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232. <script>$("#miasto").html(geoip_city());</script>
  233. <div id="miasto"></div>
  234.  


tak działa cały skrypt
nospor
masz diva o ID miasto i sie dziwisz ze ci sie zapisuje w DIV..... Prosilem o caly kod po zmianach. PO zmianach mial byc input i VAL a nie HTML
maksik
chciałem ci wyłącznie pokazać kod w którym to działa

po zmianach wygląda to tak i nie działa:

  1. <script>$("#miasto").value(geoip_city());</script>
  2. <input type="text" id="miasto" value="">
nospor
Jak juz pisalem w poprzedim poscie ma byc VAL a nie VALUE
maksik
nic nie zmieniło
nospor
Nic nie zmienilo czyli co? Nadal w div ci sie pisze? Przeciez w kodzie co pokazales nie masz DIV....
Masz to gdzies wystawione? W ten sposob w zyciu do niczego nie dojdziemy
maksik
kod html wygląda wyłącznie w ten sposób przy czym nie wyświetla wyniku w input text'cie

  1. <script>$("#miasto").val(geoip_city());</script>
  2. <input type="text" id="miasto" value="">
nospor
No ale zmien kolejnosc.... najpierw input, a dopiero potem przypisanie do niego
maksik
Bardzo jestem wdzięczny, problem zażegnano.

Mam jeszcze tylko jedno pytanie odnośnie wyświetlanego wyniku miasta. Chciałbym go uwzględnić przy pobieraniu wyników z bazy typu
  1. ... WHERE lokalizacja='".$miasto"');


jak mogę ten wynik umieścić do postaci $miasto ?
nospor
Musisz to pole wyslac do php. Albo normalnie formularzem, albo AJAXem
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.