JAVASCRIPT - 根据从下拉列表中选择的选项验证输入 [英] JAVASCRIPT - validate inputs based on option selected from dropdown

查看:64
本文介绍了JAVASCRIPT - 根据从下拉列表中选择的选项验证输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这个代码,我被要求写,我已经取得了很大进展,但在这一小部分已经走到了尽头。所以我们的规范说有一个下拉栏(一个选择选项)我实现如下;



So I have this code that I was asked to write, and I've progressed a lot but have reached a dead end in this small part. So our specification says to have a drop down bar(a select option) which I have implemented as follows;

<label>State

    <select name="State">
        <option value="VIC">VIC </option>
        <option value="NSW">NSW</option>
        <option value="QLD">QLD</option>
        <option value="NT">NT </option>
        <option value="WA">WA </option>
        <option value="SA">SA </option>
        <option value="TAS">TAS </option>
        <option value="ACT">ACT</option>
    </select>

</label>





所以基本上你选择一个州,然后在下一个字段中你需要输入你的邮政编码。但是,



SO basically you choose a state, and then in the next field you need to type in your postal code. But,

The selected state must match the first digit of the postcode
VIC = 3 OR 8, NSW = 1 OR 2 ,QLD = 4 OR 9 ,NT = 0 ,WA = 6 ,SA=5 ,TAS=7 ,ACT= 0
(e.g. the postcode 3122 should match the state VIC)





我不知道该怎么做。



我的代码的下一部分看起来像这样;



And I have no idea how to go about doing this.

the next part of my code just looks like this;

<p>
					<label for="pcode">Post Code</label>
				
					<input id="pcode" type="text" name="pcode" size="4" />
					
				</p>





我尝试过:



我尝试过多种方法,例如在文本框前面创建一个选项,我输入邮政编码,根据我选择的状态动态更改其中的选项但遇到问题因为我还是个新手。这实际上是我的任务的最后一部分,大约需要4个小时,我真的可以使用一些帮助!在此先感谢你们!



What I have tried:

I've tried multiple things such as creating a select option in front of the text box where I type in the postal code that dynamically changes the options within it based on the state I select but ran into issues because I'm still a novice at this. This is literally the last part of my assignment thats due in about 4 hours and I really could use some help! Thanks in advance guys!

推荐答案

对你的问题的一些建议:

首先,在你的选择标签中将数字设置为值

ie。

few suggestion to your problem:
First of all, set number as value in your select tag
ie.
<SELECT>
 <option value="1"> first state </option>
 <option value="2"> second state </option>
 <option value="3"> third state </option>
</SELECT>





现在通过查看String.indexOf函数与第一个字符匹配,你会打赌



替代解决方案:

如果你想将值保持为字符串,当前声明的方式,那么你需要保留一个数据集可以代表这个;

示例:



Now matching with first character with the try looking at String.indexOf function, you will bet

Alternative solution:
If you want to keep value as string, the way have currently declared, then you need to keep an data set that can represent this;
example:

// example 1
var stateResult = new Object();
stateResult['StateOne'] = 1;
stateResult['StateTwo'] = 2;
stateResult['StateThree'] = 3;

// example 2
var stateResult = {StateOne: 1, StateTwo: 2, StateThre: 3};

// read the data
console.log(stateResult.StateOne);
console.log(stateResult["StateTwo"]);


这篇关于JAVASCRIPT - 根据从下拉列表中选择的选项验证输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆