1

Windows 7/8 用の無人 XML ファイルを作成する Web サイトに取り組んでいます。

ユーザーがプロダクト キーを入力できる "プロダクト キー" テキスト入力を作成しました。XML に追加されるため、インストール中にプロンプ​​トが表示されません。

これは私がこれまでに持っているものです:

<td>Product Key:</td>
<td><input id="ProductKey1" size="5" maxlength="5" type="text">-<input id="ProductKey2" maxlength="5" size="5" type="text">-<input id="ProductKey3" maxlength="5" size="5" type="text">-<input id="ProductKey4" maxlength="5" size="5" type="text">-<input id="ProductKey5" maxlength="5" size="5" type="text"></td>

基本的に、最大長が 5 文字の 5 つのテキスト入力を追加し、各フィールドは「-」で区切られています。

私がやりたいのは、プロダクト キー用に 1 つの長いフィールドを用意し、5 文字ごとに "-" を挿入することです。これを JavaScript で実装する最も簡単な方法は何でしょうか?

4

3 に答える 3

2

ダッシュをシミュレートして入力フィールドに配置し、onkeyupコントロールを追加してスペースを追加するか、jquery.maskedinput:http ://digitalbush.com/projects/masked-input-plugin/

于 2013-03-08T14:51:34.040 に答える
0

これを達成するには多くの方法があります。しかし、私がすることは、5 文字ごとに文字列を「substr」することです。私はこれを次のようにします:

function split_product_key(t) {
    // How many characters in string
    var length = t.length;
    // How many "5 chars length strings" is there in your string
    var loop = Math.ceil(length/5);
    // Create the output.
    var output = '';
    for (var i =1; i<=loop; i++) {
        // Getting the 5 chars substr
        output += t.substr((i*5),5) + '-';
    }
    // Removing the last "-"
    output = output.substr(0, output.length-1);
    return output;
}

あとは、ユーザーが入力したプロダクト キーを取得し、次のように「split_product_key」に渡すだけです。リアルタイムで値を更新したい場合は、入力にも変更を加えて、入力値を split_product_key(current_input_value) にするだけです。

于 2013-03-08T15:05:59.830 に答える