在HTML中调用单独的Javascript函数
问题描述:
我想用一个参数作为html变量来调用一个单独的Javascript函数。在HTML中调用单独的Javascript函数
代码:
<h4>Enter your name:</h4>
<!-- the text box -->
<input type="text"id="name"value="">
<!-- this is the button to submit the text -->
<input type="button" value="Submit"
onclick="inp = parseFloat(document.getElementById('name').value);
<!-- This is where I call the function -->
qual(inp,4);">
这是它分析由用户输入的文本的“QUAL”功能的一部分:
if(Capp == 4) {
switch(inpt) {
case "first_text":
<!-- this is allowing this name to be allowed into the site -->
allow[4] = 0;
break
case "second_text":
<!-- 0 means true 1 means false -->
allow[4] = 0;
break
default:
allow[4] = 1;
}
}
计算机在所述第一参数取为“ inp“,当我想要输入用户输入的文本时。基于“允许”数组的html文档的输出是00011或“true,true,true,false,false”。我做了一些挖掘,发现函数正在采用“inp”,我不知道如何让文档将其识别为变量。
我该怎么做?
- 伊恩·S.
答
从看你的代码,我会说你的问题是onClick
,你试图传递一个未声明的变量在事件处理程序。您最好在onClick
中调用qual
函数,并在函数本身中获取所需的值。
这会打电话给你的质量fun
希望有所帮助。
答
你将不得不包裹“INP”周围的JavaScript函数eval()
所以这将是eval(inp)
,但你想通过这些文章,了解的eval()
What are the security issues with “eval()” in JavaScript?“邪恶”
只是出于好奇,为什么你不只是调用读取值的函数onSubmit?似乎(从代码中)你对html属性放置了很多东西? – ggdx
是的,是否有一个特殊的原因,没有一个单独的JavaScript文件加载HTML处理所有的动态性? HTML并不意味着要做逻辑。 –