来自文本字段的值没有返回任何东西
问题描述:
我想构建一个小应用程序,用户在其中输入一个商店名称,该商店名称将成为图像的替代文本以及它们的商店的URL,它们将变为ah参考然后将生成一些复制和粘贴代码,以在他们的网站上使用。来自文本字段的值没有返回任何东西
我遇到的问题是,当试图使用香草JavaScript读取商店名称的值没有任何显示。
请参阅我的小提琴: http://jsfiddle.net/willwebdesigner/gVCcm/
$(document).ready(function() {
// Creates a method and prototype for the Array for splicing up words
Array.prototype.removeByValue = function(val) {
for(var i=0; i<this.length; i++) {
if(this[i] == val) {
this.splice(i, 1);
break;
}
}
}
var myStore = {
storeName: document.getElementById("storeName").value,
Url: document.getElementById("yourURL"),
instructions: "<p>Please copy the code above and paste it into your webpage to display the shop4support logo and a link to your store.</p>",
blackLogo: function() {
return "<img src="https://www.shop4support.com/Resources/home/information/InformationForProviders/Availables4s-bk.jpg" alt="" + this.storeName + " is available on shop4support" />";
}
}
$("#urlForm").submit(function(e) {
// Clear output form first
$(output).html(' ');
e.preventDefault();
// Create an array for conditional statements
var address = [];
address = $(myStore.Url).val().split("www");
// Need to put a conditional in here to detect if https://
if(address[0] === "https://") {
$(output)
.slideDown()
.prepend("<a href=""+ $(myStore.Url).val() + "">" + myStore.blackLogo() + "</a> ")
.append(myStore.instructions);
} else if(address[0] === "http://") {
// Remove the http part off the URL
var removeHttp = address;
removeHttp.removeByValue('http://');
$(output)
.slideDown()
.prepend("<a href="https://www" + removeHttp + "">" + myStore.blackLogo() + "</a> ")
.append(myStore.instructions);
} else {
$(output)
.slideDown()
.prepend("<a href="https://"+ $(myStore.Url).val() + "">" + myStore.blackLogo() + "</a> ")
.append(myStore.instructions);
}
});
});
感谢威尔
答
更新你的代码为:
$("#urlForm").submit(function(e) {
myStore.storeName= (document.getElementById("storeName").value);
/* The rest of your code */
}
答
的那一刻你是初始化的MyStore对象中的值未填写呢。您必须在提交时执行此操作。
所以,你可以在var myStore = {}
移动到提交回调:
答
初始化您的MyStore变量:
$("#urlForm").submit(function(e) {
var myStore = {}
});
谢谢,这一直奏效对我来说是有道理的。你能告诉我为什么你把document.getElementById包装在括号里吗?我删除它们,它工作得很好。 – 2013-04-05 09:19:48
什么都没有,我只是提醒了数值并改变了它,忘了更改括号! – 2013-04-05 10:16:02