0

ページの一部として検証を記述しようとしています。これにより、テキストボックスに必要なテキストが含まれているかどうかを確認し、ボタンがない場合はボタンを無効にします。

私はこれを次のjQueryコードを使用して完全に機能させています:

 <script>
 $(document).on("ready", function () {
     $('input[type="text"]').on('blur', function () {
        var $required = $(this).next().val();
        var $image = $(this).parent().next().children('img');
        var $errors = $('img:visible').length;
        if (($(this).val().indexOf($required) == -1) && ($(this).val() != '')) {
            $image.show();
            $("#submit").prop("disabled", true);
        } else {
            $image.hide();
            //Manually subtract from error otherwise it doesnt subtract until the next focus change
            $errors--;
            if ($errors == 0)
                $("#submit").prop("disabled", false);
        }
    });
});

</script>

この問題は、テキストボックスに2つの値が必要な場合に発生します。これらは、コンマ区切りの文字列として非表示の入力に格納されます。両方が存在することを確認するにはどうすればよいですか?それらが両方ともそこにある限り、それらは任意の順序でそこにあることができます。.split('、')を使用してそれらを取得し、配列してテストできることはわかっていますが、それが問題になっています。

.inArray()と.each()の両方を使用してみましたが、どこにも到達できません。

フィドルを設定しようとしましたが、機能しませんでした。

これは、MVCプロジェクトとしてのHTMLの縮小版であるため、このほとんどはモデルなどによって入力されています。

HTML

  <table width="100%" border="1px solid">
    <tr>
        <th>Source Text</th>
        <th>Translation</th>
    </tr>

    <tr>
        <td>Some text here</td>
        <td><input type="Text"><input type="hidden" value="Some"></td>
        <td><img id="cross" src="../cross.png" class="jq-cross hid" alt="error" /></td>
    </tr>

    <tr>
        <td>Some text here</td>
        <td><input type="Text"><input type="hidden" value="Some,Here"></td>
        <td><img id="cross" src="" class="jq-cross hid" alt="error" /></td>
    </tr>
<tr><td colspan="3"><button id="submit" type="submit">Update Translations</button>  </td></tr>
 </table>

あなたが私に与えることができるどんな助けでも最もありがたいです。うまくいけば、私はここで十分な情報を提供しました。

前もって感謝します

4

3 に答える 3

2
$(document).on("ready", function () {
     $('input[type="text"]').on('blur', function () {
        var $required = $(this).next().val().split(','); // split values with comma
                                                         // and make array
        var $image = $(this).parent().next().children('img');
        var $errors = $('img:visible').length;
        var value = $.trim( this.value );           // get blur text field value

        // checking value with inArray()

        if ( $.inArray(value, $required) ) {
            $image.show();
            $("#submit").prop("disabled", true);
        } else {
            $image.hide();
            //Manually subtract from error otherwise it doesnt subtract until the next focus change
            $errors--;
            if ($errors == 0)
                $("#submit").prop("disabled", false);
        }
    });
});
于 2012-07-17T15:56:42.220 に答える
2

For...Inステートメントを使用するだけです:

for(x in $required)
{
    if($(this).val().indexOf($required[x]) !== -1)
    {
        // Code in case if text don't have required string
    }
}
于 2012-07-17T15:40:31.283 に答える
1

次のようなカンマ区切りの文字列をループする関数を作成できます。

function isTextInText(text, searchtext) {
    var arr = text.split(',');

    for(var i=0, len=arr.length; i < len; i++) {
        if (searchtext.indexOf(arr[i]) == -1) return false;
    }
    return true;
}

console.log(isTextInText("hello","hello there")); //true   
console.log(isTextInText("hello,there","hello there")); //true
console.log(isTextInText("hello,bye","hello there")); //false

次に、if ロジックは次のようになります。

if (isTextInText($required,$(this).val())

フィドル - http://jsfiddle.net/e7qkd/

于 2012-07-17T15:59:39.060 に答える