在jQuery对话框中单击保存按钮时,将变量发送到函数?

时间:2015-05-13 04:15:45

标签: jquery dialog

我有

<td>
    <?php $var=$arrayD[ 'Structural Data Loaded']; echo "<a data-id='$id' class='StructuralDataLoaded'>" . $var . "<img  id='$id' class='img' onclick='javascript:SelectStatus(id);' src='images/edit.png'></a>"; ?>
</td>

function SelectStatus(id) {

    var idvalue=id;
        console.log(idvalue); /* I can see the idvalue at console */
    var SelectingStatus = $('#SelectingStatus');
    SelectingStatus.dialog({
        close: function(event, ui) {

        },
        modal: true,
        title: 'id' ,
        width: 600,
        height: 'auto',
        overlay: {
            backgroundColor: '#000000',
            opacity: 0.5
        },
        buttons: {
          'Save' : function PostStructuralDataLoadedData(idvalue){
    var data = $('#selected option:selected').val();
    console.log(idvalue);/* I can not see the idvalue at console */
    $(this).dialog('destroy');
    var request = $.ajax({
    url: "InsertData.php",
    type: "POST",
    data: { 
    id : idvalue,
    data : data,
    type : "StructuralDataLoladed"
    },
    dataType: "html"
    });

    request.done(function( ) {
    location.reload();
    });
    request.fail(function( jqXHR, textStatus ) {
    alert( "Request failed: " + textStatus );
    });
},
          'Return': function(){$(this).dialog('destroy');}
      }

    });// End AddDocument dialog

}

当点击Save按钮并调用PostStructuralDataLoadedData函数时,我想将datavalue发送到该函数,我在PostStructuralDataLoadedData函数中编写了console.log(datavalue),但我在控制台中看不到任何内容。如何将datavalue发送到PostStructuralDataLoadedData函数?

3 个答案:

答案 0 :(得分:0)

我建议不要使用id尝试将控件作为参数发送到您的函数,如下所示:

onclick='javascript:SelectStatus(this)'

并在您的函数中

function SelectStatus(ctrl) {

    var idvalue=$(ctrl).attr('id'); //get the id attribute
    console.log(idvalue); 
    ........
    ........
}

答案 1 :(得分:0)

您需要将$id传递给onclick='javascript:SelectStatus($id);而不是id传递给onclick='javascript:SelectStatus(id);'

应该是 -

<?php $var=$arrayD[ 'Structural Data Loaded']; echo "<a data-id='$id' class='StructuralDataLoaded'>" . $var . "<img  id='$id' class='img' onclick='javascript:SelectStatus('$id');' src='images/edit.png'></a>"; ?>

答案 2 :(得分:0)

我找到了灵魂。因此,当我调用PostStructuralDataLoadedData函数时,我应该像它一样:

       function PostStructuralDataLoadedData()

所以必须删除idvalue。