3

私は、ユーザーが画像の代替テキストになるストア名と、参照になるストアの URL を入力する小さなアプリを構築しようとしています。 Webサイト。

私が抱えている問題は、バニラ 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 "&lt;img src=&quot;https://www.shop4support.com/Resources/home/information/InformationForProviders/Availables4s-bk.jpg&quot; alt=&quot;" + this.storeName + " is available on shop4support&quot; /&gt;";
        }
    }

    $("#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("&lt;a href=&quot;"+ $(myStore.Url).val() + "&quot;&gt;" + myStore.blackLogo() + "&lt;/a&gt; ")
                .append(myStore.instructions);

        } else if(address[0] === "http://") {
            // Remove the http part off the URL
            var removeHttp = address;
            removeHttp.removeByValue('http://');
            $(output)
                .slideDown()
                .prepend("&lt;a href=&quot;https://www" + removeHttp + "&quot;&gt;" + myStore.blackLogo() + "&lt;/a&gt; ")
                .append(myStore.instructions);

        } else {
            $(output)
                .slideDown()
                .prepend("&lt;a href=&quot;https://"+ $(myStore.Url).val() + "&quot;&gt;" + myStore.blackLogo() + "&lt;/a&gt; ")
                .append(myStore.instructions);
        }
    });
});

感謝します

4

3 に答える 3

3

myStore オブジェクトを初期化している時点では、値はまだ入力されていません。提出時にこれを行う必要があります。

var myStore = {}したがって、送信コールバックに移動できます。

http://jsfiddle.net/gVCcm/1/

于 2013-04-05T08:14:35.507 に答える