我正在构建一个小的文本编辑器,该编辑器不仅可以编辑文件,还可以创建一个新文件。这就是我的选择菜单的配置方式。
<select name="reportname" id="reportname" class="form-control">
<option value="zrxqy">--Choose a Report--</option>
<option value="newReport">--Create a New Report--</option>
<optgroup label="General Reports">
<option name="Volumes" id="Volumes" value="volumes.php">Volumes</option>
<option name="Options" id="Options" value="Options.php">Options</option>
</optgroup>
</select>
<textarea>
在此下方有一个显示所选报告文件的内容。id="reporttxt"
。当select
被改变,我想textarea的内容清除,然后将新文件内容放在这个作品文件之间切换时,但如果用户输入文本,它打破了清算。
我已经试过了:
$("#reportname").change(function(){
$("reporttxt").html('') //this works when switching between functions
//function to import new text <-- this works fine
}
我也尝试过这个:
$("#reportname").change(function(){
if($(this).val() == "newReport"){
$("#reporttxt").val(''); //this clears user input but now nothing will display
} else {
$("#reporttxt").html('');
}
//function to import text <-- does not display text after .val('') is fired
});
函数导入文本将其插入为:
$.ajax({
type: "GET",
url: "report-data.php",
data: "file="+$(this).val(),
datatype: "json",
success: function( result ) {
$("#reporttxt").append(result.body);
}
});
有什么想法为什么会发生这些奇怪的事情?
编辑
问题其实是.append()
。
我不知道为什么,但是改变了
$("#reporttxt").append(result.body);
到
$("#reporttxt").val(result.body);
解决了问题。我现在只是$("#reporttxt").val('');
要清除。
发布答案...就像在change函数中一样,调用val()在文本区域上设置文本。.这是为了回答为什么append()不起作用。append()只是将另一个节点添加到HTML DOM中的调用者节点。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句