HTML/Javascript问题
我在为网站编写代码时遇到了一个问题。它是用HTML/Javascript编写的。我已经成功地编写了大量代码,这些代码似乎工作正常,但现在的问题是,我似乎无法在Javascript中使用多行字符串HTML/Javascript问题,javascript,html,function,Javascript,Html,Function,我在为网站编写代码时遇到了一个问题。它是用HTML/Javascript编写的。我已经成功地编写了大量代码,这些代码似乎工作正常,但现在的问题是,我似乎无法在Javascript中使用多行字符串 <!DOCTYPE html> <html> <head> <title>Multiline test</title> </head> <body> <p>Select variable value be
<!DOCTYPE html>
<html>
<head>
<title>Multiline test</title>
</head>
<body>
<p>Select variable value below:</p>
<div>
<form name="form001">
<select name="choice">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</form>
</div>
<p id="selection"></p>
<script type="text/javascript">
// First, get the <select> element. findElementsByName() returns a collection,
// we only want the first elements that's found (hence the [0]):
var choice = document.getElementsByName('choice')[0];
// Now, get a reference to the <p> where we'll show the result:
var selectionP = document.getElementById('selection');
// This Array will hold the labels. label[0] will be 'Text1', labels[1] 'Text2', etc.
var labels = [
"Multiline test \n Multiline test",
"Text2",
"Text3"
];
// Now attach a handler to the onchange event.
// This function will be executed if the <select>ion is changed:
choice.onchange = function() {
var optionIndex = choice.selectedIndex; // The index of the selected option
var text = labels[optionIndex]; // The label that corresponds to that index
selectionP.innerHTML = text;
};
</script>
</body>
多行测试
选择下面的变量值:
1.
2.
3.
//首先,获取元素。findElementsByName()返回一个集合,
//我们只需要找到的第一个元素(因此是[0]):
var choice=document.getElementsByName('choice')[0];
//现在,获取的参考,我们将在其中显示结果:
var selectionP=document.getElementById('selection');
//此数组将保存标签。标签[0]将为“Text1”,标签[1]“Text2”等。
变量标签=[
“多行测试\n多行测试”,
“文本2”,
“文本3”
];
//现在将处理程序附加到onchange事件。
//如果离子发生变化,将执行此功能:
choice.onchange=函数(){
var optionIndex=choice.selectedIndex;//所选选项的索引
var text=labels[optionIndex];//与该索引对应的标签
selectionP.innerHTML=文本;
};
这是更新的代码。现在我只需要一个多行解决方案。
document.form001.choice
"Text" + choice + "Text"}
没有任何意义,是吗?你需要像这样做
var choice = document.form001.choice
"Text" + choice + "Text"}
为了遵循JavaScript程序的流程,您应该使用Google Chrome的JavaScript控制台。这确实有助于理解发生了什么。我在代码中重写了问题区域,并添加了注释以显示更改的内容
<!DOCTYPE html> <!-- HTML pages should have a DOCTYPE header, as well as html, head, and body tags. -->
<html>
<head>
<title></title>
</head>
<body>
Text
Text
Text
Text
<br/>
<br/>
Select variable value below:
<br/>
-
<div>
<form name="form001">
<select name="choice">
<!-- <size=3> Don't think there's a such thing as a size tag -->
<option value=1 selected="selected">1</option>
<option value=2>2</option>
<option value=3>3</option>
</select>
<script type="text/javascript">
function js001(){ //This was missing paranthesis
var v1 = "Text1"
var v2 = "Text2"
var v3 = "Text3"
var choice = document.form001.choice; //choice wasn't defined
alert("Text" + choice + "Text");
}
</script>
<script type="text/javascript">
//js001();
</script> <!-- The S needed to be lowercase here -->
<div class="pagetext">
<br/>
<br/>
Text
<br/>
<br/>
Text
<div>
</body>
</html>
正文
正文
正文
正文
选择下面的变量值:
-
1.
2.
3.
函数js001(){//缺少此函数
var v1=“Text1”
var v2=“Text2”
var v3=“Text3”
var choice=document.form001.choice;//未定义选项
警报(“文本”+选项+“文本”);
}
//js001();
正文
正文
注意:我在这里提供了一个该代码的工作示例:
让我们看一下下面的HTML:
<html>
<head></head>
<body>
<p>Select variable value below:</p>
<div>
<form name="form001">
<select name="choice">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</form>
</div>
<script type="text/javascript">
// JavaScript goes here
</script>
</body>
</html>
到目前为止和我在一起?好
现在,您想根据选择显示“Text1”、“Text2”或“Text3”,对吗?因此,我们必须知道选择了哪个
。这也很容易:
var optionIndex = choice.selectedIndex;
这将为您提供所选
的从零开始的索引。因此,如果选择第一个选项,optionIndex
将具有值0
要根据选择显示一些文本,我们需要一些字符串。因为我们在这里处理的是一个集合,所以让我们把它放在一个数组中
:
var labels = [
"Text1",
"Text2",
"Text3"
];
JavaScript中的数组也是基于零的,因此标签[0]将是“Text1”,标签[1]将是“Text2”,等等
如果我们把这一切都放在一起,我们会得到这样的结果:
var choice = document.getElementsByName('choice')[0];
var labels = [
"Text1",
"Text2",
"Text3"
];
choice.onchange = function() {
var optionIndex = choice.selectedIndex; // The index of the selected option
var text = labels[optionIndex]; // The label that corresponds to that index
alert(text);
};
我希望这有帮助。:-) 我注意到你写了:
现在我相信正在发生的是当它不调用
Javascript的功能与预期的一样
在您的函数中:
写:
console.log('this function is being executed');
// this will make a line show up in the chrome document inspector / firebug console (just google those)
或
这将有助于排除故障。我认为另一个版本可以满足您的需要 当用户从下拉列表中选择项目时,它会在下拉列表下方显示一个文本字符串,该字符串对应于用户选择的项目
<html>
<body>
<form name="form001">
<!-- when the user changes the selected item in this dropdown, -->
<!-- the JavaScript function "js001()" will get called -->
<select name="choice" onchange="js001()">
<option value=0 selected="selected">1</option>
<option value=1>2</option>
<option value=2>3</option>
</select>
</form>
<!-- This is the div in which the selected item's related text will be shown -->
<div id="selectedStuff"/>
<script type="text/javascript">
function js001()
{
// Rather than use separate variables for each value,
// it is simpler if we store them in an array
var values = ["Text1", "Text2", "Text3"];
// The value of the selected item in the dropdown maps to the index in the values
// array for the text we want to show
var valueIndex = document.form001.choice.value;
var text = "Text" + values[valueIndex] + "Text"
document.getElementById("selectedStuff").innerHTML = text;
}
// When the page loads it makes sense to call the js001() function,
// so that the text for initially-selected dropdown value gets shown
js001();
</script>
</body>
</html>
1.
2.
3.
函数js001()
{
//而不是对每个值使用单独的变量,
//如果我们将它们存储在一个数组中,会更简单
变量值=[“Text1”、“Text2”、“Text3”];
//下拉列表中选定项的值映射到值中的索引
//要显示的文本的数组
var valueIndex=document.form001.choice.value;
var text=“text”+值[valueIndex]+“text”
document.getElementById(“selectedStuff”).innerHTML=text;
}
//当页面加载时,调用js001()函数是有意义的,
//以便显示初始选定下拉列表值的文本
js001();
wow:O-首先从脚本标记内部删除html(除非在字符串内部)。var a='';发布标记以及最终结果。很抱歉,nikc,我“我对这些语言相当陌生,我不知道你的意思。接下来,写Javascript,而不是VB。例如,变量是var foo=value;
,不能以数字开头。和function js001(){
@Madmartigan这是我在这里粘贴代码时造成的问题。我的错误是,它已被更改。Javascript仍然没有显示。而且,选择是一个元素,而不是一个值。不确定这是否是有意的,但我没有理会它。我只是在错误下方划线,没有真正解释如何从selec中取出值t
tag,但是是的,你是对的。你需要使用document.form001.choice.value
,因为choice
是一个对象,带有一个value
属性,该属性包含从下拉列表中选择的值。我认为document.form001.choice
是一个数组,因此你应该在获取之前指定你关注的选项.value
属性ou
alert('This function is being executed!')
<html>
<body>
<form name="form001">
<!-- when the user changes the selected item in this dropdown, -->
<!-- the JavaScript function "js001()" will get called -->
<select name="choice" onchange="js001()">
<option value=0 selected="selected">1</option>
<option value=1>2</option>
<option value=2>3</option>
</select>
</form>
<!-- This is the div in which the selected item's related text will be shown -->
<div id="selectedStuff"/>
<script type="text/javascript">
function js001()
{
// Rather than use separate variables for each value,
// it is simpler if we store them in an array
var values = ["Text1", "Text2", "Text3"];
// The value of the selected item in the dropdown maps to the index in the values
// array for the text we want to show
var valueIndex = document.form001.choice.value;
var text = "Text" + values[valueIndex] + "Text"
document.getElementById("selectedStuff").innerHTML = text;
}
// When the page loads it makes sense to call the js001() function,
// so that the text for initially-selected dropdown value gets shown
js001();
</script>
</body>
</html>