簡體   English   中英

如何將變量選擇器傳遞給jquery .attr()

[英]How to pass a variable selector to jquery .attr()

這是方案。 在jQuery DataTable中,我有一些值和一個編輯按鈕來修改其中的2個。 單擊它,將打開一個模式彈出窗口,並提供一個表格以插入新的注釋和狀態值:

...

"columnDefs": [
                        {
                           "targets": [ 0 ],
                           "visible": false
                           //"searchable": false
                        }],

                "columns":  [
                    { "data": "id" },
                    { "data": "date" },
                    { "data": "type" },
                    { "data": "name" },
                    { "data": "user_name" },
                    { "data": "status" },
                    { "data": "closing_date" },
                    { "data": "info" },
                    { "data": "note" },
                    { "data": null,
                          "render": function ( data, type, full, meta ) {

                            return "<button type='button' class='btn btn-info btn-md' id=\'" + full.id + "\' data-toggle='modal' data-id=\'" + full.id + "\' data-target='#myModal'> Edit </button>";

                      }

...

在下面,我有ajax函數將插入的值發送到Spring控制器:

//This function is used to send the form data to Spring controller; cause we use a modal, with code that must be put in the file with html table head,
    //we must replace the use of view made by jsp using an ajax function
    $('#myModal').on('click', '.btn.btn-success', function(event) {
        var form = $('#updateeventsform'); //recover the form inside modal by using id
        var formdata = form.serializeObject(); //use the serializeObject function to prepare data for Json format
        formdata.idevent = $(this).attr('data-id'); //add the event id to form data, after setting it with the IDnumber variabile
        console.log(formdata, this);
        event.preventDefault();

        //here starts the code to sending data to Spring controller
        $.ajax({
                url: "../updateevents.json",
                type: "post",
                data: formdata,
                success : function() {

                    console.log("Invio riuscito.");
                    DTevents.ajax.reload( null, false ); //we reload the table, showing immediately the data updated.
                }

            });

        });

這段代碼在formdata.idevent上給了我一個未定義的值。 沒錯,因為$(this)值引用了當前元素(在我的例子中是Submit按鈕)。 如您所見,按鈕的id是一個數字值,由full.id字段設置。 因此,我嘗試了一下:將一個數值作為attr()函數的選擇器。 我變了:

formdata.idevent = $(this).attr('data-id');

formdata.idevent = $('#5').attr('data-id');

這可行。

因此,問題是:有沒有辦法將變量值用作.attr()函數的selector 如果否,我該如何使用將正確的值傳遞給控制器​​?

編輯評論答案。

已經使用$(this).data('id'); 不起作用。 我獲得了idevent的不確定值。

<button type='button' class='btn btn-info btn-md' **id=\'" + full.id +**

在這里,您可以注意到按鈕元素的數字ID。

目的是:該表表示事件。 當必須將兩個字段(注釋和狀態)修改后發送給控制器時,我還必須向其發送事件ID,並且我想使用.attr()函數來執行此操作。 現在,必須在選擇器上使用此函數; 我可能想將按鈕ID用作選擇器,但我有具有不同ID的不同按鈕。 因此,如果我單擊第四個按鈕,則ID為4,我可能有:

formdata.idevent = $['#4'].attr('data-id');

如果單擊第5個按鈕,則代碼必須為:

formdata.idevent = $['#5'].attr('data-id');

如果我單擊第六個按鈕:

formdata.idevent = $['#6'].attr('data-id');

等等。 因此,我有一個變量選擇器可以使用; 我不知道該怎么做。

我會嘗試向模式添加隱藏的輸入...

<input type="hidden" id="idevent" name="idevent">

然后在“編輯”按鈕上單擊,將其id攜帶到模態form

$(".btn.btn-info").on("click",function(){
  var idevent = $(this).data("id");    // Or $(this).attr("id")

  // Delay for the modal openning animation...
  setTimout(function(idevent){
    $('#myModal form #idevent').val(idevent);
  },800);
});

然后,當用戶提交信息時,該信息將已經處於表單中。
;)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM