开发者

JQuery - Clear Textboxes

开发者 https://www.devze.com 2023-02-12 08:51 出处:网络
I have a drop down list and two textboxes on an ASP.Net MVC 3 view. The drop down lists numbers from 1 to 10. The two textboxes are both wired up to use the JQuery Datepicker.

I have a drop down list and two textboxes on an ASP.Net MVC 3 view. The drop down lists numbers from 1 to 10. The two textboxes are both wired up to use the JQuery Datepicker.

When the user selects an option from the drop down, I would like JQuery code to clear the values in the two textboxes.

I have written the JQuery code below which does just this, however, when I go to submit the form details to the post method within my controller, the dates I have selected for the two textboxes are 开发者_StackOverflowcleared.

This is my code to date:

$(document).ready(function () {


$('#equipment_warrantyLength').change(ResetDates); //fires when drop down changed

function ResetDates() {
    alert("hello");

    $("#equipment_purchaseDate").val(''); // clear textbox
    $('#equipment_warrentyExpires').val(''); //clear textbox

}

});

The textboxes should only be cleared when the drop down is selected, but currently, they also clear when the user attempts to submit the form.

Any feedback would be much appreciated.


When the form is submitted to the server the page is reloaded. Unless you set the submitted values in the textboxes upon loading the page again they are cleared. ASP.NET MVC doesn't use Viewstate like ASP.NET WebForms so unless you manually init the fields they will be empty when pages are reloaded after a form submission.

Update:

You can also submit the form data via jQuery. In order to do that you would do something like this:

  1. Add a click handler to the submit button. (If it is declared as you should also change it to a normal button so that the browser wont submit the form automatically.
  2. In the click handler add code to do the ajax submission. See this question for tips on that Submit form using AJAX and jQuery

But en the end it might be easier to just put the submitted data in the viewmodel and reinitialize the fields in the view after submission.

0

精彩评论

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

关注公众号