如何检查在php中点击的按钮的ID

时间:2017-01-25 05:45:28

标签: php jquery ajax

我有两个按钮 using System; using System.Collections.Generic; using System.Web.Services; using Newtonsoft.Json; namespace WebApplication1 { /// <summary> /// Summary description for wsSymptoms /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. [System.Web.Script.Services.ScriptService] public class SympsService : System.Web.Services.WebService { /// <summary> /// /// </summary> [Serializable] public class symps { public String Sympt { get; set; } public symps (String newval) { Sympt = newval; } public symps() { } } /// <summary> /// This method explicitly serialized the data /// which means it has to be json parsed on the client. /// /// </summary> /// <param name="organ_name"></param> /// <returns></returns> [WebMethod(enableSession:false)] public string GetSymptomsSerialized(String organ_name ) { // I prefer Newton serializer over microsoft one. The ms on adds more overhead and // causes issue particularly if an object is making a round trip to the client. List<symps> list = fakedData(); String serializedList = JsonConvert.SerializeObject(list); return serializedList; } /// <summary> /// This method lets the framework take care of the serialization. /// This works but its not my personal prefered method /// </summary> /// <param name="organ_name"></param> /// <returns></returns> [WebMethod(enableSession:false)] public List<symps> GetSymptomsObject(String organ_name) { return fakedData(); } // since i am not hooked up to a database, just made stuff up. private List<symps> fakedData() { List<symps> list = new List<symps>(); list.Add(new symps("discolor")); list.Add(new symps("pimples")); list.Add(new symps("sorness")); list.Add(new symps("pain")); list.Add(new symps("break")); list.Add(new symps("twig")); list.Add(new symps("red")); list.Add(new symps("green")); list.Add(new symps("discolor")); return list; } } } "removeD"(按ID),我为它们编写了相同的ajax,如下所示:

"updateRecord"

但是在tableDisplay.php中我希望两个按钮都有不同的功能。如何查看在php中点击的按钮的ID? 我尝试过使用: $.ajax({ url: 'DB/tableDisplay.php', type: 'POST', data: 'id='+uid, dataType: 'html' }) 但这不起作用。

4 个答案:

答案 0 :(得分:1)

试试这个:

for (int i = 0; i < mList.Count; i++)
{
    m = mList[i];
    //foreach (string value in m.GetGenreList())
    //{
        string all_genres = string.Join(",", m.GetGenreList()); //get all genres as a comma seperated in single string
        Console.WriteLine("{0,-10} {1,-30} {2,-10} {3,-20} {4,-20} {5,-10}", i + 1, m.Title, m.Duration, all_genres, m.Classification, m.openingDate);
    //}
}

答案 1 :(得分:0)

尝试使用以下代码检测单击的按钮:

<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
    $(document).ready(function () {
        $('.click-button').on('click',function () {
            var button_id = $(this).attr('id');
            if(button_id == 'removeD'){
                alert('removeD button clicked');
            }else if(button_id == 'updateRecord'){
                alert('updateRecord button clicked');
            }

        });


    });
</script>

</head>

<input type="button" class="click-button" id="removeD" value="removeD">

<input type="button" class="click-button" id="updateRecord" value="updateRecord">

答案 2 :(得分:0)

        $('body').on('click','#id1',function(){
          $.ajax({
             url : 'url',
             data : {variable:values},
             type : 'html',
             dataType : 'GET/POST',
             success : function(data){
               console.log('Message after Success');
    },
             error : function(){
               console.log('Error Message')
    }

});
});

在您的网址页面中,您可以找到是否发布了ajax请求

    if(isset($_REQUEST['variable'])){
        write your php code here........
}

答案 3 :(得分:0)

您可以使用target返回哪个DOM元素触发了该事件。看下面的代码,它太容易和短,无法获得点击元素的id。

 $('button').click(function(e){
      alert(e.target.id);
 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="remove">Remove</button><br>
<button id="update">Update</button><br>