2016-09-09 10 views
4

私はそのテキストエリアから値を挿入しようとすると、私は動的なテキストエリアを作成しています。それは私にヌル値を与えます。考える動的テキストエリアから値を取得

<form name="myForm"> 
<textarea name="fname" <%#!((GPNS.BusinessLayer.SpecialItems.SpecialItem)Container.DataItem).Code.Equals("OTH", StringComparison.InvariantCultureIgnoreCase) ? "style='display: none;'" : string.Empty%> id="text<%#((GPNS.BusinessLayer.SpecialItems.SpecialItem)Container.DataItem).ID%>" maxlength="50" placeholder="Enter other item details"></textarea> 
</form> 

TEXTAREAボックスから値を取得するための私の関数である。

function ValidateData() { 
      if ($("textarea").is(":visible")) { 
       //var x = document.forms["myForm"]["fname"].value; 
       var x = document.getElementsByName("fname").value; 
       if (x == null || x == "") { 
        alert("Please Enter Other Item Details"); 
        return false; 
       } 
      } 
      else return true 
     } 
+2

getElementsByNameは「はドン、要素のリストを取得しますvalueプロパティを持っています。 –

答えて

2

あなたのテキストアレイは動的なので、テキストエリアの変更イベントを使用できます。

var OtherItemValue; 
$("textarea").on('input change keyup', function() { 
       if (this.value.length) { 
        OtherItemValue = this.value; 
       } else { 
        OtherItemValue = ""; 
       } 
      }); 

そしてあなたは、コードの下に使用することができます:あなたは、負荷にそれはOtherItemValueに設定されますので、いつでもあなたが入力したテキストを与えられたコードを使用することができます

function ValidateData() { 
    if ($("textarea").is(":visible")) { 
if (OtherItemValue == null || OtherItemValue == "") { 
      alert("Please Enter Other Item Details"); 
      return false; 
     } 
    } 
    else return true 
} 
+0

ありがとう@Bhupendraは動作しています – Developer

0
<form name="myForm"> 
<textarea name="fname" <%#!((GPNS.BusinessLayer.SpecialItems.SpecialItem)Container.DataItem).Code.Equals("OTH", StringComparison.InvariantCultureIgnoreCase) ? "style='display: none;'" : string.Empty%> id="text<%#((GPNS.BusinessLayer.SpecialItems.SpecialItem)Container.DataItem).ID%>" maxlength="50" placeholder="Enter other item details"></textarea> 
</form> 

あなたの機能は、今、あなたのコードの問題は、そのgetElementsByNameあるこの

function ValidateData() { 
     if ($("textarea").is(":visible")) { 
      //var x = document.forms["myForm"]["fname"].value; 
      var x = document.getElementsByName("fname")[0].value; 
      if (x == null || x == "") { 
       alert("Please Enter Other Item Details"); 
       return false; 
      } 
     } 
     else return true 
    } 

のようになります。 valueプロパティを持たない要素のリストを取得します。あなたは特定の要素だけを取得し、その価値を得る必要があります。この解決法はあなたの問題を解決するかもしれませんが、テキストエリアの上にfnameという名前の他の要素がない場合にはうまくいきます。

+0

textareaにはすでに動的IDがあります。これを2回定義することはできません。 – Developer

+0

@Developerが今すぐチェックします。 –

+0

ヘルプ@Rachitありがとうございますが、動作していません – Developer

0

すでにjQueryを使用しているので、希望の結果を得るために使用しないでください。あなたのコードは次のようになります

:あなたは本当に標準ライブラリを使用する必要がある場合

function ValidateData() { 
    if ($("textarea").is(":visible")) { 
     var x = $("textarea").val(); 
     if (x == null || x == "") { 
      alert("Please Enter Other Item Details"); 
      return false; 
     } 
    } 

    return true 
} 

、@Rachit Guptaのanswerは、問題を解決する必要があります。

+0

それは動作していません – Developer

+0

それは動作し、ここに証拠があります:https://jsfiddle.net/4ecvb0o8/1/ – mdziekon

+0

問題が「結果」として「真」を得ていない場合は、 else''キーワード - 必要ではなく、場合によってはコードが '' undefined''を返すように強制します。 – mdziekon

0

jqueryの.val()メソッドを使用して、テキスト領域の値を取得します。 文書準備機能でテキストエリアを空にする必要があります。

$(document).ready(function(){ 
    $("textarea[name='fname']").val("");  
}); 

    function ValidateData() { 

    if ($("textarea").is(":visible")) { 
     var x = $("textarea[name='fname']").val(); 
     if (x == null || x == "") { 
      alert("In if " + x); 
      return false; 
     } 
     else { 
       alert("In else" + x); 
     } 
    } 
    else { 
      return true 
    } 
} 
+0

@Developerは上記のコードを試すことができます –

+0

それは働いていません@DPT – Developer

+0

@Developer @私はこれを試すことができます私の答えを更新しました。 –

関連する問題