如何基于带有提交按钮的选定选项调用条件函数
How to call a conditional function based on a selected option with a submit button?
我希望用户首先从下拉选择菜单中选择他们想要的顺序(数字-字母-数字,字母-数字-字母等),然后按提交按钮调用该函数,从而随机生成字母和数字的组合。函数从数组中随机抽取。然后,根据用户选择的选项,以这种方式组合它们并将其返回给用户。
我以前写过类似的代码,不涉及条件或选择菜单。相反,它只是一个用户按下调用函数的按钮。该函数有两个或多个数组,这些数组使用Math.floor ( Math.random() * listOne.length );
成功随机化。随机化后,该函数将使用简单的listOne[partOne] + listTwo[partTwo]
设置将各部分组合在一起。
undefined
给用户。我以前从未使用过条件语句或选择菜单,所以我不知道我做错了什么。如果您能解释并提供一个解决方案,我将不胜感激。<script type="text/javascript">
function myFunction()
{
var x=document.getElementById("result");
var listOne = Array();
var listTwo = Array();
var number = Math.floor ( Math.random() * listOne.length );
var letter = Math.floor ( Math.random() * listTwo.length );
listOne[0] = "1";
listOne[1] = "2";
listOne[2] = "3";
listTwo[0] = "A";
listTwo[1] = "B";
listTwo[2] = "C";
var select = document.getElementById("mySelect");
var order = select.options[select.selectedIndex].value;
x.innerHTML = Result
if (order === 1)
{var Result = listOne[number] + listTwo[letter] + listOne[number];}
if (order === 2)
{var Result = listTwo[letter] + listOne[number] + listTwo[letter];}
if (order === 3)
{var Result = listOne[number] + listTwo[letter] + listTwo[letter];}
if (order === 4)
{var Result = listTwo[letter] + listOne[number] + listOne[number];}
if (order === 5)
{var Result = listOne[number] + listOne[number] + listTwo[letter];}
if (order === 6)
{var Result = listTwo[letter] + listTwo[letter] + listOne[number];}
}
</script>
<div id="result"><i>Your result will appear here</i></div>
<select id=mySelect>
<option value="1">one</option>
<option value="2">two</option>
<option value="3">three</option>
<option value="4">four</option>
<option value="5">five</option>
<option value="6">six</option>
</select>
<input type="submit" onClick="myFunction();" value="Generate" />
由于Result
尚未定义的事实,您正在获得undefined
。以下是您可能需要进行的更改。
由于您正在使用===
进行评估,因此需要将order
变量设置为int
var order = parseInt(select.options[select.selectedIndex].value);
下一步,移动x.innerHTML = Result
到底部。
下面是最终结果(稍微整理一下):
function myFunction() {
var x = document.getElementById("result");
var listOne = Array();
var listTwo = Array();
var number = Math.floor(Math.random() * listOne.length);
var letter = Math.floor(Math.random() * listTwo.length);
var select = document.getElementById("mySelect");
// casting it as an int.
// an alternative would be var order = +select.options[select.selectedIndex].value
var order = parseInt(select.options[select.selectedIndex].value);
var Result;
listOne[0] = "1";
listOne[1] = "2";
listOne[2] = "3";
listTwo[0] = "A";
listTwo[1] = "B";
listTwo[2] = "C";
if (order === 1) {
Result = listOne[number] + listTwo[letter] + listOne[number];
}
if (order === 2) {
Result = listTwo[letter] + listOne[number] + listTwo[letter];
}
if (order === 3) {
Result = listOne[number] + listTwo[letter] + listTwo[letter];
}
if (order === 4) {
Result = listTwo[letter] + listOne[number] + listOne[number];
}
if (order === 5) {
Result = listOne[number] + listOne[number] + listTwo[letter];
}
if (order === 6) {
Result = listTwo[letter] + listTwo[letter] + listOne[number];
}
// add html AFTER Result has been set
x.innerHTML = Result
}
DEMO
给你的提交按钮一个ID #submit
<input id="submit" type="submit" value="Generate" />
这就是你所需要的:
function myFunction(){
var $result = document.getElementById("result");
var $select = document.getElementById("mySelect");
var order = $select.options[$select.selectedIndex].value;
var listOne = ["1","2","3"]; // now it has length
var listTwo = ["A","B","C"];
var randNum = Math.floor ( Math.random() * listOne.length );
var randChar = Math.floor ( Math.random() * listTwo.length );
var res = "";
if (order === "1"){
res = listOne[randNum] + listTwo[randChar] + listOne[randNum];
}
if (order === "2"){
res = listTwo[randChar] + listOne[randNum] + listTwo[randChar];
}
if (order === "3"){
res = listOne[randNum] + listTwo[randChar] + listTwo[randChar];
}
if (order === "4"){
res = listTwo[randChar] + listOne[randNum] + listOne[randNum];
}
if (order === "5"){
res = listOne[randNum] + listOne[randNum] + listTwo[randChar];
}
if (order === "6"){
res = listTwo[randChar] + listTwo[randChar] + listOne[randNum];
}
$result.innerHTML = res;
}
document.getElementById("submit").onclick = myFunction;
您可以将选项元素的值设置为字符串,如'nln'
,以表示序列'数字字母数字'。
函数可以是这样的:
function getRandomString(s) {
var a;
var t = '';
var data = {n: [0,1,2,3], l: ['a','b','c','d']};
s = s.split('');
for (var i=0, iLen=s.length; i<iLen; i++) {
a = data[s[i]];
t += a[Math.random()*a.length | 0];
}
return t;
}
上面的值可以在"随机"字符串中重复。要获取唯一值,请使用splice从数组中获取值,而不是使用普通的属性访问:
t += a.splice(Math.random()*a.length | 0, 1);
HTML可以是:
<form onsubmit="document.getElementById('result').innerHTML=getRandomString(this.mySelect.value);return false;">
<select id=mySelect>
<option value="nln" selected>one</option>
<option value="lnl">two</option>
<option value="nll">three</option>
<option value="lnn">four</option>
<option value="nnl">five</option>
<option value="lln">six</option>
</select>
<input type="submit"value="Generate">
</form>
<div><span id="result"></span></div>
试试这个:
工作演示:JSFiddle
HTML:<div id="result"><i>Your result will appear here</i></div>
<form name="myform">
<select id="mySelect">
<option value="1">one</option>
<option value="2">two</option>
<option value="3">three</option>
<option value="4">four</option>
<option value="5">five</option>
<option value="6">six</option>
</select>
<input id="calc" type="button" value="Generate" />
</form>
function myFunction() {
var x=document.getElementById("result");
var listOne = [1, 2, 3];
var listTwo = ["A", "B", "C"];
var select = document.getElementById("mySelect");
var order = +(select.options[select.selectedIndex].value);
var number = Math.floor ( Math.random() * listOne.length );
var letter = Math.floor ( Math.random() * listTwo.length );
var result;
if (order == 1) {
result = listOne[number] + listTwo[letter] + listOne[number];
}
if (order === 2) {
result = listTwo[letter] + listOne[number] + listTwo[letter];
}
if (order === 3) {
result = listOne[number] + listTwo[letter] + listTwo[letter];
}
if (order === 4) {
result = listTwo[letter] + listOne[number] + listOne[number];
}
if (order === 5) {
result = listOne[number] + listOne[number] + listTwo[letter];
}
if (order === 6) {
result = listTwo[letter] + listTwo[letter] + listOne[number];
}
x.innerHTML = result;
}
//Call the function;
document.getElementById("calc").onclick = myFunction;
相关文章:
- 我们如何在互联网断开连接或用户关闭选项卡/浏览器时调用注销servlet
- 在JavaScript中调用函数时自定义此选项
- 在新的浏览器选项卡上加载新的aspx页面后调用JavaScript函数
- 无法在其他调用上设置jQuery对话框选项
- 如何在页面加载angular js上设置从ajax调用中选择的选项
- 给出<选项>标记一个类?API调用不工作
- Jquery Chosen Ajax调用未呈现选项
- 调用函数单击选择标签中的选项
- 设置间隔未在选项卡内容脚本中调用
- 在单击浏览器选项卡上调用 js 函数
- 更改调用函数时选择框的选项值
- 用于确定 AJAX 调用的引导选项卡选择
- 如何在新建的_blank选项卡上调用函数
- 在angularjs中更改选择选项时调用函数
- 如何根据所选选项调用实例方法
- 使用 AngularJS 需要选项调用另一个指令
- 是否可以使用一些自定义选项调用$('form').validationEngine('valid
- 从选择框选项调用 JavaScript
- 避免在更新选项调用中清理画布
- 如何基于带有提交按钮的选定选项调用条件函数