简体   繁体   English

更新表单元素而无需提交(Ajax + JS)

[英]Update Form Element Without Submit ( Ajax + JS )

I have to update certain elements of the form from database (using an external php file) without submitting. 我必须从数据库(使用外部php文件)更新表单的某些元素,而无需提交。

For example: write employee id and the name would be populated before even submitting. 例如:写出员工ID,甚至在提交之前就填写姓名。 I'm pretty new to ajax hence didn't quite find how to execute the same. 我对ajax很陌生,因此没有找到如何执行相同的方法。

Bits and pieces of my code are as follows: 我的代码片段如下:

part of xyz.js xyz.js的一部分

$("#makeproj-nav").click(function()
{

// project form starts

s = "<form id = 'create_new_form' name = 'new_form' method='post' action='insert_new.php'><div class='project_form'><span>";

s += "<div class='Form_contents'> Employee ID: <input type='text' size='100' name='emp_id'></div>";

s += "<div class='Form_contents'> Employee Name: <input type='text' size='100' name='emp_name'></div>";

s += "<div class='Form_contents'> <input type='submit'> </div>";
s += "<span></div></form>";

$("#card-wrapper").html(s);

$('#create_new_form').on('click', function(e) {

$.ajax({
      url : 'Find_EmpName.php',
      type : 'POST',
      data : $(this).serialize(),
      success : function(response) {
        $("emp_name").value(response.reply);
      }
    });
  });

});

Find_EmpName.php should ideally take the employee id and then should return employee name and update it in the desired field. Find_EmpName.php在理想情况下应获取员工ID,然后应返回员工名称并在所需字段中对其进行更新。 Would be great if you rectify the code above. 如果您更正上面的代码,那就太好了。 Thanks 谢谢

First of all, 首先,

you .submit() a form, you don't .click() it. .submit()形式,你没有.click()它。

You click the submit button in the form to submit the form, BUT YOU DONT CLICK A FORM ITSELF. 您单击表单中的提交按钮以提交表单, 但不要单击表单本身。

So first change click to submit unless you want your ajax call to be executed every time someone clicks on your form. 因此,首先更改clicksubmit除非您希望每次有人单击您的表单时都执行ajax调用。

Then, use .preventDefault() to disable normal submit behavior on the submit event, and do your ajax call once someone clicks the submit button or submits the form. 然后,使用.preventDefault()禁用对.preventDefault()事件的正常提交行为,并在有人单击“ submit按钮或提交表单后进行ajax调用。

$('#create_new_form').on('submit', function(event){
    event.preventDefault();
    //your ajax call
});

Next, 下一个,

When you do this: 执行此操作时:

$("emp_name").value(response.reply);

Specifically, this: 具体来说,这是:

$("emp_name")

You are asking jQuery to select all elements that are <emp_name> 您要让jQuery选择<emp_name>所有元素

Is there such a thing? 有这样的事吗? I didn't think so. 我不这么认为。

So, what you want to do is target an input by name, right? 因此,您要做的是按名称定位输入,对吗?

So you do this: 所以你这样做:

$("input[name=emp_name]")

Next, 下一个,

There is no .value() in jQuery, it's .val() . jQuery中没有.value() ,它是.val() Do not mix javascript and jQuery. 不要混合使用javascript和jQuery。

The correct version would be this: 正确的版本是这样的:

$("#create_new_form input[name=emp_name]").val(response.reply);

You can find more information about how to use selectors here: 您可以在此处找到有关如何使用选择器的更多信息:

http://api.jquery.com/category/selectors/ http://api.jquery.com/category/selectors/

Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM