2011-07-01 8 views
0

このコード出力は、ボタンをクリックすると表示されるテキストボックスにコンテンツを入力するものです。しかし、私は、のチェックボックスを選択してから、レンダリングするテキストボックスに入力した値を欲しいと思っています。JQuery/Ajax-MVC2を使用してボタンクリックイベントとしてチェックボックスを使用する方法

//View page: In this page JQuery for request&Responce type and i am displaying two text box and 1 check box and 1 button 

<script type="text/javascript"> 

    $(function(){ 
     $('#selectAll').change(function() { 
      alert("value changed");//When i run this script only witht this alert function i am getting output but with this code i am unable to get the output what i expect 

      var options = { 
     target: '#result-user', // id of the div where we are going to display result 
       beforeSubmit: showRequest, 
       success: showResponse, 
       type: 'post', 
       resetForm: true 
      }; 
      $('#form-user').ajaxForm(options); // id of the form we wish to submit 
     }); 
    }); 


     function showRequest(formData, jqForm, options) { 
      $("#result-user").empty().html('Loading....'); 
      $("#form-user :input").attr("disabled", true); // disable all form inputs while loading 
     } 



     function showResponse(responseText, statusText, xhr, $form) { 
      $("#result-user").empty().html(responseText); 
      $("#form-user :input").attr("disabled", false); 
     } 
    </script> 
</head> 

<body> 

    <% using (Html.BeginForm("Index","Home",FormMethod.Post, new { id="form-user", name="form-user"})) {%> 

     <fieldset> 
      <legend>Fields</legend> 
<div class="editor-label"> 
       <%= Html.LabelFor(model => model.FirstName) %> 
      </div> 
      <div class="editor-field"> 
       <%= Html.TextBoxFor(model => model.FirstName) %> 
       <%= Html.ValidationMessageFor(model => model.FirstName) %> 
      </div> 
     <div class="editor-label"> 
       <%= Html.LabelFor(model => model.LastName) %> 
      </div> 
      <div class="editor-field"> 
       <%= Html.TextBoxFor(model => model.LastName) %> 
       <%= Html.ValidationMessageFor(model => model.LastName)%> 
      </div> 
      <div> 
      <input type="checkbox" name="selectl" value="ON" id="selectAll"/> 
      </div> 
      <p> 
       <input type="submit" value="Save" /> 
      </p> 

Controlページ

public class HomeController : Controller 
{ 
    // 
    // GET: /Home/ 

    public ActionResult Index() 
    { 
     return View(); 
    } 

    [HttpPost] 
    public ActionResult Index(HomeModel data) 
    { 
     return Content("You submitted: " + data.FirstName + " " + data.LastName); 

    } 

} 

モデルページ

public class HomeModel 
{ 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 

} 

このコードの実際の出力は、ボタンを押したときに、コンテンツがテキストボックスに入力して表示することです。しかし、私は出力が好きですwheチェックボックスを選択すると、テキストボックスに入力した内容をレンダリングする必要があります。

友人に教えてください。

答えて

0

ajaxFormフォームを送信しません。送信ボタンをクリックすると、ajaxを介して送信するようにフォームが設​​定されます。

変更からajaxSubmit(options)に変更してください。それはトリックを行う必要があります(ちょうどそれを試して、それが動作します)。申し訳ありません

+0

ありません運.. ..私を示唆 – User1234

+0

は私が私のチェックボックスに任意の変更を行う必要がありますしてください、私は文字通り新しいMVCソリューションにあなたのコードを貼り付け/コピーすることでこれを見つけた

User1234

+0

属性。 ajaxFormをajaxSubmitに変更すると、探していた動作が発生しました。 – rossipedia

関連する問題