【发布时间】:2014-08-19 01:28:08
【问题描述】:
我是一个初学者,正在尝试从头开始编写一些东西:我想将文本字段中的输入“写入”到文本区域。 所以我做了以下 (也可以在http://jsfiddle.net/jolarti/FX6xL/1/ 上查看)
HTML:
<input type='text' id='fruit' value='fruit'>
<br/>
<input type='text' id='salad' value='salad'>
<p>This is the meal you made:</p>
<textarea id="myText" rows="4" cols="50"></textarea>
<br />
<button>Make food</button>
脚本:
$(document).ready(function () {
$("#fruit");
$("#salad");
$("button").click(function () {
var fruit = $("#fruit").val();
var salad = $("#salad").val();
if (fruit === "" || salad === "") {
alert("Type in both ingredients in order to make something!!");
} else {
//alert("Would you like " + fruit + " with some " + salad + " to have for lunch?");
var phrase = ("Would you like " + fruit + " with some " + salad + " to have for lunch?");
document.getElementById("myText").innerHTML = phrase; //writes to the textarea
return false; //i use this to end the script and not make the page disappear
}
});
});
我在这里使用“return”的目的是否正确? 它可以工作并且可以满足我的要求,但是...这是好的做法还是不好的做法? 我把这个'return'放进去,因为如果我不这样做,页面在输入后就完全空了! + 我需要解释为什么会发生这种情况。 我想知道:我是否也可以使用 'return' 将输出实际写入屏幕?
【问题讨论】:
-
是的,它也可以而且很常见。但是要aware of this。
标签: javascript jquery return