2012-07-17 81 views
0

我想寫一些驗證作爲我的頁面的一部分,確保如果文本框有一些必需的文本,它將禁用該按鈕,如果它不存在。如何使用jQuery檢查文本框中的所有元素?

我有這方面的工作完全使用下面的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> 

當文本框需要兩個值出現問題。它們作爲逗號分隔的字符串存儲在隱藏的輸入中。我如何檢查兩者都存在?只要他們都在那裏,他們可以以任何順序在那裏。我知道我可以使用.split(',')來獲取並排列和測試它們,但那是我粘在的部分。

我已經嘗試使用.inArray()和.each(),但只是無法得到它的任何地方。

我試圖設置一個小提琴,但無法讓它工作。

下面是HTML作爲一個MVC項目的縮減版本,所以大部分是由模型等填充

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> 

任何幫助,您可以給我會非常感謝。希望我在這裏提供了足夠的信息。

在此先感謝

+0

如果你只是標記像'所需的輸入字段很多你的邏輯將被簡化<所需的輸入>'(或者甚至只是''在HTML 5中),然後將它用於你的事件處理程序選擇器,比如'('input'[data-required]:text')。on('blur',fn)'。 – 2012-07-17 15:39:02

回答

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/

+0

這個解決方案對我來說非常合適。非常感謝。 – 2012-07-18 08:25:04

2

只需使用的For ... in語句:

for(x in $required) 
{ 
    if($(this).val().indexOf($required[x]) !== -1) 
    { 
     // Code in case if text don't have required string 
    } 
} 
+0

他可能需要用逗號分隔'$ required'。 – 2012-07-17 15:44:03

+2

他已經提到過它。我只給了他一個答案,如何用JavaScript檢查這個數組中的每個元素。 – mdziekon 2012-07-17 15:45:40

+0

你是對的,對不起,我錯過了。 – 2012-07-17 15:47:28

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); 
     } 
    }); 
}); 
+0

感謝您的幫助,但這不起作用。問題是所需的文本不是文本框中唯一可以進入的文本,如果我輸入除必需的文本以外的文本以及所需的文本,則測試將失敗。 – 2012-07-18 08:17:10

相關問題