Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/429.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我可以用javascript+html制作国家和城市的动态列表吗_Javascript_Html - Fatal编程技术网

我可以用javascript+html制作国家和城市的动态列表吗

我可以用javascript+html制作国家和城市的动态列表吗,javascript,html,Javascript,Html,我有一个城市动态列表,每个国家都有自己的城市动态列表 现在我用Javascript检查邮政编码列,并尝试创建一个函数来进行下一次搜索,当我选择荷兰时,它允许我在邮政编码中使用其他字符,但直到现在我还没有看到任何成功 有人能告诉我我做错了什么吗 HTML: Javascript: 这是您的代码: <input type="text" name="postalcode" class="columnstyle" placeholder="Enter your postal code" oncha

我有一个城市动态列表,每个国家都有自己的城市动态列表

现在我用Javascript检查邮政编码列,并尝试创建一个函数来进行下一次搜索,当我选择荷兰时,它允许我在邮政编码中使用其他字符,但直到现在我还没有看到任何成功

有人能告诉我我做错了什么吗

HTML:

Javascript:

这是您的代码:

<input type="text" name="postalcode" class="columnstyle" placeholder="Enter your postal code" onchange="postalcode(this.id,'Country')"/>
这是错误的,因为函数postalcode中的'Country'参数不清楚。此输入类型为文本,因此获取This.id也是无效的,我建议使用以下解决方案:首先,选择一个选项,让用户选择国家:

<label>choose your country</label>
<select name="country onchange="userCountry(this.value)">
<option value="country_a">a</option>
<option value="country_b">b</option>
</select>
第二个创建输入,供用户键入带有其国家/地区图案的邮政编码:2a创建功能创建邮政编码图案:

function userCountry(country) {
   switch (country) {
      case a:
      limitLength = 6;
      // you could set up your regex for input validation
      regExp = /\d{3,6}/;
      break;
      case b:
      limitLength = 8;
      break;
      // etc.
   };
   document.getElementById("postalCode").setAttribute("maxlength",limitLength);
};
// here is the input in html for user to input the postal code
<input type="text" name="user_postalcode" id="postalCode" onkeyup="validationCode()"/>
// validation the input, only necessary if you have regexp
function validationCode() {
   var invalidCode = /[^\d]+/g; // wrong pattern you make your own, in this case, all characters beside digital character will be considered invalid.
   if (this.value.match(regExp) == null) { // check if it's wrong
      this.value.replace(invalidCode,""); // replace all invalid code with space, so it will remain only valid code
   };
};

我希望这能有所帮助,我已经试着理解你的问题,这是我从你的问题中理解的。

你有错误吗?此代码是否运行?确保没有错误发生或任何警报在函数的第一行中放置警报。它显示了吗?不,我真的不知道为什么,我的意思是我写函数start workf onclick,我甚至尝试将其更改为onsumbit和onchange,但什么都没有。您正在传入id,但实际上没有在html中指定id。这会导致一个问题,因为代码变量不能得到设置我真的不明白你的意思,这个输入类型是文本,但在我的另一个函数中,比如在动态列表中,我在html中得到了两个选择,在国家选择中,我像这里这样做了。id,“城市”和所有的工作都很好,也许如果你能解释一点你所说的不清楚这个输入类型是文本的意思,我会更清楚。谢谢你帮助我。
<label>choose your country</label>
<select name="country onchange="userCountry(this.value)">
<option value="country_a">a</option>
<option value="country_b">b</option>
</select>
function userCountry(country) {
   switch (country) {
      case a:
      limitLength = 6;
      // you could set up your regex for input validation
      regExp = /\d{3,6}/;
      break;
      case b:
      limitLength = 8;
      break;
      // etc.
   };
   document.getElementById("postalCode").setAttribute("maxlength",limitLength);
};
// here is the input in html for user to input the postal code
<input type="text" name="user_postalcode" id="postalCode" onkeyup="validationCode()"/>
// validation the input, only necessary if you have regexp
function validationCode() {
   var invalidCode = /[^\d]+/g; // wrong pattern you make your own, in this case, all characters beside digital character will be considered invalid.
   if (this.value.match(regExp) == null) { // check if it's wrong
      this.value.replace(invalidCode,""); // replace all invalid code with space, so it will remain only valid code
   };
};