我最近开始学习 Javascript 以帮助我工作,但我不禁认为我已经贪得无厌了。
想法:
使用表单,获取一条输入的信息,使用预定义值对信息进行前缀和后缀,并将其输出到文本区域,以便我可以复制/使用它。
出于某种原因,我发现到目前为止学习 javascript 特别困难,而且我编写的代码确实有效,正是我想要的,但我忍不住认为我编写的代码令人难以置信草率。
表格
<form name="invalidateForm" action="">
Image Name:<br /><input id="imageName" type="text" name="imageName" value=""><br />
<input class="btn" type="button" onclick="invalidateUrls()" value="Generate URLs">
<input class="btn" type="reset"><br />
<textarea id="icdnResults"></textarea>
</form>
JavaScript
<script type="text/javascript">
function invalidateUrls()
{
var txt = "";
document.getElementById("icdnResults").value = "";
if (document.getElementById("imageName").value != ""){
txt = "";
//Category Thumbnails
txt += "prefix text";
txt += document.getElementById("imageName").value;
txt += "suffix text\n";
//Product Page Main Image
txt += "prefix text 2";
txt += document.getElementById("imageName").value;
txt += "suffix text 2\n";
//Flyout image on product page
txt += "prefix text 3";
txt += document.getElementById("imageName").value;
txt += "suffix text 3\n";
//Product page thumbnails
txt += "prefix text 4";
txt += document.getElementById("imageName").value;
txt += "suffix text 4\n";
document.getElementById("icdnResults").value += txt;
}
}
</script>
我 99% 确信我所做的事情比草率更进了一步,而且我也猜测这根本不是“面向 future ”的。
我应该更多地使用和定义变量,还是有完全不同的方法?
如果有人能判断我是否走在正确的轨道上,或者我是否应该完全放弃它,我将不胜感激。
谢谢:)
亚历克斯
编辑
我向读到这篇文章的人道歉,我似乎没有正确解释并忘记提及一件关键的事情;前缀和后缀都有 4 个不同的字符串。
因此,用户将在输入字段中输入一个值,并且会使用 4 个不同的值作为前缀,并使用 4 个不同的值作为后缀。
http://www.somedomain.com/folder/?fmy=<<USER_INPUT>>&type=etc
http://www.somedomain.com/folder/?someCmd=<<USER_INPUT>>&layer=etc
http://www.somedomain.com/folder/?itemId=<<USER_INPUT>>&value=etc
http://www.somedomain.com/folder/?pageR=<<USER_INPUT>>&caption=etc
那么“?”在哪里?有4个不同的值,其中“&”也有4个完全不同的值。
编辑2
好的,根据约翰的回复......我稍微调整了一下。
看起来怎么样?我第一次尝试更改代码只是垃圾?
var processForm = function(){
var value = $('#imageName').val(),
outputElement = $('#icdnResults'),
html = '',
s_imageTag = function(size){
return size + value;
},
e_imageTag = function(size2){
return size2 + "\n";
},
size = ['s_catThumb', 's_productMain'],
size2 = ['e_catThumb', 'e_productMain'];
html += s_imageTag('s_catThumb ');
html += e_imageTag(' e_catThumb');
html += s_imageTag('s_productMain ');
html += e_imageTag(' e_productMain');
outputElement.val(html);
};
$(function(){
$('#gen').on('click', processForm);
});
最佳答案
如上所述,但不使用额外的库。 JQuery非常棒,如果您要执行大量 JavaScript 操作,您可能会发现它更容易使用。但如果您不想添加对其他库的依赖,也可以使用 vanilla JS 来完成:
HTML
<form name="invalidateForm" action="">Image Name:<br />
<input id="imageName" type="text" name="imageName" value=""><br />
<input class="btn" type="button" value="Generate URLs" id="gen">
<input class="btn" type="reset"><br />
<textarea id="icdnResults"></textarea>
</form>
JS
var processForm = function () {
var value = document.getElementById('imageName').value,
html = '',
imageTag = function (size) {
return 'prefix ' + size + ' <<' + value + '>> suffix ' + size + "\n";
},
sizes = ['text', 'text 2', 'text 3'];
for (var i = 0; i < sizes.length; i++) {
html += imageTag(sizes[i]);
}
document.getElementById('icdnResults').value = html;
};
document.getElementById('gen').addEventListener('click', function() {
processForm();
}, false);
如我的答案和上面的答案所示;使用 unobtrusive javascript 是一个很好的做法在可能的情况。这将使您的代码保持干净并提高可读性和可维护性。
此外,如果您希望支持 Internet Explorer 8 及更早版本,则需要将 addEventListener 替换为处理其支持的函数,请参阅这篇文章 addEventListener in Internet Explorer .
关于javascript - 使用 Javascript 将预定义字符串添加到使用表单的用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19821574/