按钮Onclick功能不将记录保存到数据库 [英] Button Onclick function is not saving record to database

查看:59
本文介绍了按钮Onclick功能不将记录保存到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

受人尊敬的朋友,

我遇到ONCLICK按钮问题。
我已经编写了将表格保存到数据库的代码。

Respected Friends,
I have problem with the ONCLICK button problem. I have written Code for saving form to the database.

图片中有2个按钮:


  • 保存按钮(类型=提交)

保存并新建按钮(类型=按钮)

保存一个非常好。使用提交类型保存数据库中的记录。

但是当涉及到 save&新的它没有保存到数据库的记录。

Save one is perfectly very fine. saving record on the database as "submit type" is used.
But when it comes to the save & new its not saving record to the database.

请任何人都可以告诉我它会出现什么问题。

Please can anyone let me know what would be the problem with it.

请在下方找到以下代码:

Please find the coding below:

 <div class="btn-lg pull-right">
    <tr>                                    
<td><input type="button" class="primarybtn" value="Save" name="save" onclick="SaveEntity();"/></td>                                 
<td><input type="button" class="newwhitebtn" value="Save and New" name="saveAndNew" onclick="SaveEntity();"/></input> </td>
<td><input type="button" class="cancelbtn" value="Cancel"></td>
                                </tr>
                            </div>

脚本代码:

Script Code:

     <script type="text/javascript">
  function SaveEntity() {   
    alert("Check Save");
      //    get the form values      
        $.ajax({
            url :"/leadstatus_creation",
            type : "POST",      
            contentType: "application/json",
            data : $('#frmCreateEntity').serialize(),
            success : function(data) { 
            if(data != null && data !='')
                {               
                    $('#txtleadname').val(data); 
                }               
            },   
            error : function(XMLHttpRequest, textStatus, errorThrown) { 
                alert(textStatus);
                alert("Lead Name Status:"+$('#txtleadname').val());
            } 
        });
    }
  </script>




任何人都可以帮我解决这个问题。

Please can anyone help me regarding this issue.

答案:

ANSWER:


我们可以使用标签而不是使用标签,而不是使用标签。

Just instead of using tag we can use tag Than its surely works.



    <td><button class="primarybtn" name="save" onclick="SaveEntity();"/>Save</button></td>
    <td><button class="newwhitebtn"  name="saveandnew" onclick="SaveEntity();"/>Save and New</button></td>
    <td><button class="cancelbtn"/>Cancel</button></td>

谢谢大家的支持。 。 :-)

Thank You All for support. . :-)

推荐答案

第二个

<td><input type="button" class="newwhitebtn" id="frmCreateEntity" value="Save and New" name="saveAndNew" onclick="SaveEntity();"/></input> </td>

在此,您还提供了与 frmCreateEntity <相同的 id / strong>和第一个一样,
这就是调用函数的原因

In this also you gave the same id of frmCreateEntity as in the first one, That is why when the function is called

            data : $('#frmCreateEntity').serialize(),

上面的代码解析了DOM的id,它如果它匹配则获得此内容

The above code parses the DOM for the id, and it when it matches it gets the content of this

< td>< input type =submitclass =primarybtnvalue =保存id =frmCreateEntityname =saveonclick =SaveEntity();/>< / td>

这就是它不保存的原因,
因为文档解析器不知道你用相同的 id定义了多少元素 as frmCreateEntity ,当它在 ID frmCreateEntity 的第一个元素中找到相同的ID时,它会停止搜索。
尝试在两个输入元素中使用不同的id实现,
肯定会有效,

<td><input type="submit" class="primarybtn" value="Save" id="frmCreateEntity" name="save" onclick="SaveEntity();"/></td> This is the reason it is not saving, As the document parser does not know for how many elements you have defined with same id as frmCreateEntity, and it stops its search when it finds that same id in the first element with ID frmCreateEntity. Try to implement with different id in both the input elements, It will definitely work,

此外,我还为您的澄清添加了一个解决方法,并获取该函数中的数据以将其保存到数据库中。
检查此代码并在此处运行

Also i have added a workaround for your clarification, and getting data in that function for saving it into your database. Check this Code and run here,

<!DOCTYPE html>
<html>
  <head>
    <title>Sample styled page</title>
  </head>
  <body>
    <div class="btn-lg pull-right">
       <td id="td_1">
         FIRST
         <input type="button" class="primarybtn" value="Save" name="save" id="id_1" onclick="SaveEntity(this);"/>
       </td>
       <td id="td_2">
         SECOND
         <input type="button" class="newwhitebtn" value="Save and New" name="saveAndNew" id="id_2" onclick="SaveEntity(this);"/></input>
       </td>
       <!-- <td><input type="button" class="cancelbtn" value="Cancel"></td> -->
    </div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

  </body>
  <script type="text/javascript">
function SaveEntity(event) {
 var obj = document.getElementById(event.id).previousSibling;
 obj.trim;
 console.log(obj.wholeText);
    //  $.ajax({
    //      url :"/leadstatus_creation",
    //      type : "POST",
    //      contentType: "application/json",
    //      data : obj,
    //      success : function(data) {
    //      if(data != null && data !='')
    //          {
    //              $('#txtleadname').val(data);
    //          }
    //      },
    //      error : function(XMLHttpRequest, textStatus, errorThrown) {
    //          alert(textStatus);
    //          alert("Lead Name Status:"+$('#txtleadname').val());
    //      }
    //  });
 }
</script>
</html>

现在你可以通过不同 id 从两个按钮事件中获取两个值。
此处还有输出屏幕截图:

Now you can get both the values from both buttons event with different id . Also here is the screenshot of the output :

希望有所帮助,

谢谢

重新编辑:

查看方式:

<td><input type="button" class="newwhitebtn" id="frmCreateEntity" value="Save and New" name="saveAndNew" onclick="SaveEntity(this);"/></input> </td>

这篇关于按钮Onclick功能不将记录保存到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆