我想構建一個小應用程序,用戶在其中輸入一個商店名稱,該商店名稱將成爲圖像的替代文本以及它們的商店的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);
}
});
});
感謝威爾
謝謝,這一直奏效對我來說是有道理的。你能告訴我爲什麼你把document.getElementById包裝在括號裏嗎?我刪除它們,它工作得很好。 – 2013-04-05 09:19:48
什麼都沒有,我只是提醒了數值並改變了它,忘了更改括號! – 2013-04-05 10:16:02