0

我的 html 表单中有一个下拉框。我希望从下拉列表中选择“0”时,应重置表单,否则进行 ajax 调用以填写表单。我为此目的编写了以下代码,但它不起作用,我的意思是表单字段没有被重置。

$("select#student_id").change(function(){
  var student = $(this).val;
  if(student!=0)
    {
      //Here I make the ajax call to populate the form
    }

在这里我如何重置表单字段。

4

5 回答 5

1
$("select#student_id").change(function(){
    var student = $(this).val();
    if(student!=0) {
        //make ajax call
    } else {
        $("form").reset();
    }
});
于 2013-09-18T11:19:24.163 回答
1
$("select#student_id").change(function(){
  var student = $(this).val;
  if(student!=0)
    {
      //Here I make the ajax call to populate the form
    }
  else
  {
    var $form = $('#form_id_here');
    $form.find("input, textarea, hidden").val("").text("");
    $form.find("input[type='checkbox'], input[type='radio']").is(":checked").attr('checked', false);
  }
});
于 2013-09-18T11:20:07.147 回答
1

它对您不起作用,因为 reset 不是 jquery 函数,您需要在 dom 元素上调用 reset 函数

$('form')[0].reset()
于 2013-09-18T11:20:45.987 回答
0
$( "#student_id" ).change(function(event, ui){
    if( $(this).val() != 0 )
    {
      //Here I make the ajax call to populate the form
      alert("Fill the form");
    }
    else {
        //reset all the fields of the form to the default values
        $(event.target.form)[0].reset();
    }
});
于 2013-09-18T11:42:51.203 回答
0

您必须说明当用户选择“0”时会发生什么。

if(student!=0)
    {
      //Here I make the ajax call to populate the form
}
else{
 //reset form
}
于 2013-09-18T11:20:03.023 回答