开发者

How to use Checkbox as button click event using JQuery/Ajax - MVC2

This Code output is what the content i enter into the text box that will display when click the button. But i want lik when i select the checkbox then the value entered in the text box to be rendered.

//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 page

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);

    }

}

Model Page

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

}

Actual output of this code is to display the content enter in the text box when hit the button.But i want output like when the select the checkBox the content which i entered in the text box should be rendered.

Please help me friends.


ajaxForm doesn't submit the form. It just sets the form up to submit via ajax when you click a submit button.

Change ajaxForm(options) to ajaxSubmit(options). That should do the trick (just tried it, and it works).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜