2017-08-16 21 views
0

おはよう。私は質問の中でほとんど全てを読んで解決しましたが、私の問題を解決することはできません。私の質問では、mvcで書かれているように、私は文字列を表示するためにコントローラからの値を渡しているし、特定の条件が満たされた場合は、モーダルを実行するjavascriptを取得します。助けてください。ありがとう。ここjavascriptに入力されたテキスト値を渡すことができません。

は私のコントローラのコードです:

public ActionResult Series() 
    { 
     List<sample> series = db.samples.Where(x => x.status == "False").ToList(); 
     if (series.Count == 0) 
     { 
      ViewBag.Info = "None"; 
     } 
     else { 
      ViewBag.Series = series; 
      ViewBag.Info = "Have"; 
     } 
     return View(); 
    } 

マイビュー:

<input type="text" value="@ViewBag.Info" id="info" name="info" /> 

マイJavascriptを:

@section Scripts{ 
<script> 
$(window).on('load', function() { 
    var modelll = document.getElementById("@(ViewBag.Info)").value; 
    var s_end = document.getElementById("myNumber2").value; 
    var s_current = document.getElementById("myNumber3").value; 
    var s_status1 = document.getElementById("status").value; 

    var s_id1 = parseInt(document.getElementById("myNumber").value); 
    var s_end2 = parseInt(s_end, 10); 
    var s_current2 = parseInt(s_current, 10); 
    var x = parseInt(s_current, 10) + 1; 

    document.getElementById("item1").value = s_id1; 
    document.getElementById("item2").value = s_end; 
    document.getElementById("item3").value = x; 
    document.getElementById("status2").value = s_status1; 

    if (modelll === 'Have') 
    { 

     if ((s_current2 > s_end2) && (s_current2 != s_end2)) { 
      $('#myModal').modal({ backdrop: 'static', keyboard: false }); 
      $('#myModal').modal('show'); 
     } 
    } 
    else 
    { 
    $('#myModal').modal({ backdrop: 'static', keyboard:false }); 
    $('#myModal').modal('show'); 
    } 

}); 
</script> 

}

+0

VAR modelll =のdocument.getElementById( "情報")の値。 –

+0

これはすでにsrを試しました。まだ動作していません –

+0

var modelll = document.getElementById( 'info'); –

答えて

0

getElementByIdをはIDが必要ですが、あなた合格です。@ViewBag.Info

var modelll = document.getElementById("info").value; 

また、実際には必要ない多くの余分な変数を作成しています。たとえばあなたがs_current2に持っているものを手に入れるために、あなたはそれが整数に変換するために、別の変数を作成するために

var s_current = parseInt(document.getElementById("myNumber3").value, 10); 

必要はありませんを使用することができます。あなたが代わりに要素IDの@ViewBag.Infoを使用しているテキストボックス

document.getElementById("info").value = var modelll;

を値を設定するには、テキストボックス

var modelll = document.getElementById("info");

から値を取得するために

+0

私はちょうどジャバスクリプトを学ぼうとしています。これでちょっと初心者です。 –

1

。 。

+0

初めてそれを試してみましたが、動作していないので、私はgetにビューバックを向けることを試みました。今私はそれがまだ動作していない更新しました –

+0

デバッガツールで要素IDを一度チェックしてください。 –

+0

私は何も疑わしいとは思わなかった –

0

次の行は、あなたのコードで問題を引き起こしている:

var modelll = document.getElementById("@(ViewBag.Info)").value; 

    // document.getElementById needs Id but you are passing @(ViewBag.Info) which is wrong 

    var modelll = document.getElementById("info").value; //info id of your textbox 
// now check 
    if (modelll === 'Have') 
    { } 
    else 
    { } 
+0

hi sr。私はvarからmodelll = document.getElementById( "@(ViewBag.Info)")を変更しました。 var modelll = document.getElementById( "info"); sr Kiran Shahiが言ったように、まだ何もありません。 –

+0

こんにちは、私はこのコードを試して、それは正常に動作していますvar modelll = document.getElementById( "info")。 if(modelll === "Have") { alert( 'hi'); アラート(モデル); }私に両方のアラートを与える – jANVI

関連する問題