0

再会,

我是一个学习 Javascript 和 Dojo 的新手,我通常通过分解运行代码的其他部分来学习。

我对如何从以下代码(来自ArcGIS Sandbox)获取子字符串值感到困惑:

    <!DOCTYPE html>
      <html>
        <head>
          <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
          <meta http-equiv="X-UA-Compatible" content="IE=7, IE=9, IE=10">
          <!--The viewport meta tag is used to improve the presentation and behavior of the samples 
            on iOS devices-->
          <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
          <title>Query State Info without Map</title>

          <script src="http://js.arcgis.com/3.6/"></script>
          <script>
            dojo.require("esri.tasks.query");
            dojo.require("esri.map");

            var queryTask, query;
            require([
              "esri/tasks/query", "esri/tasks/QueryTask",
              "dojo/dom", "dojo/on", "dojo/domReady!"
            ], function(
              Query, QueryTask,
              dom, on
            ){
              queryTask = new QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5");

              query = new Query();
              query.returnGeometry = false;
              query.outFields = ["SQMI","STATE_NAME","STATE_FIPS","SUB_REGION","STATE_ABBR","POP2000","POP2007","POP00_SQMI","POP07_SQMI","HOUSEHOLDS","MALES","FEMALES","WHITE","BLACK","AMERI_ES","ASIAN","OTHER","HISPANIC","AGE_UNDER5","AGE_5_17","AGE_18_21","AGE_22_29","AGE_30_39","AGE_40_49","AGE_50_64","AGE_65_UP"];

              on(dom.byId("execute"), "click", execute);

              function execute(stateName) {
                query.text = dom.byId("stateName").value;
                //execute query
                queryTask.execute(query, showResults);
              }

              function showResults(results) {
                var s = "";
                for (var i=0, il=results.features.length; i<il; i++) {
                  var featureAttributes = results.features[i].attributes;
                  for (att in featureAttributes) {
                    s = s + "<b>" + att + ":</b>  " + featureAttributes[att] + "<br>";
                  }
                  s = s + "<br>";
                }
                dom.byId("info").innerHTML = s;
              }
            });
          </script>
        </head>
        <body>
          US state name : 
          <input type="text" id="stateName" value="California">
          <input id="execute" type="button" value="Get Details">
          <br />
          <br />
          <div id="info" style="padding:5px; margin:5px; background-color:#eee;">
          </div>
        </body>
      </html>

我想做的就是分离输入(在本例中是 id="stateName" ,即加利福尼亚这个词)。

所以一个愚蠢的例子是用下面的代码来获取当有人输入“加利福尼亚在西海岸”时的前 10 个字符

     query.text = dom.byId("stateName").substring(0,10);

这真的是我可以支持其他查询,但我想如果我可以在这个输入上做一个子字符串,那么当我查询其他属性时它真的是一样的。

提前感谢新手!

4

3 回答 3

1

您需要获取innerHTMLDOM 元素的

query.text = dom.byId("stateName").value.substring(0, 10);
于 2013-09-20T18:24:55.990 回答
0

如评论中所述,调用.value将提供您需要的内容。Substring 是字符串原型上的一个方法,请参见此处。但是,dom.byId返回一个 domNode。您不想要 domNode 本身的子字符串,您想要 domNode 文本值的子字符串。在输入上,这很容易完成,.value并且通常也可以使用.textContentand完成.innerHTML

于 2013-09-20T21:46:35.533 回答
0

正如托马斯厄普顿正确指出的那样,正确的形式是:

      dom.byId("stateName").value.substring(0, 10);

显然以下也有效

     dom.byId("stateName").value.substr(0, 10);
于 2013-09-20T21:45:06.350 回答