如何通过单击JQuery按钮来提取id数据?

时间:2016-08-01 16:43:50

标签: javascript php jquery

我可以从执行此方法的模式$('#ID').on('shown.bs.modal', function (e) { var somevariable = e.id; });中提取数据,但无法通过单击按钮来确定如何执行此操作。

如果可以,如果单击此按钮打开模态,我怎样才能继承数据?

http://www.bootply.com/jCXXE6chXu

$(document).ready(function(){
  $(".file_to_upload").click(function (data) {
  //alert("works");
      var questionID = data.id;
    alert(questionID);
    $('[id*="uploaded_files_"]').modal('show');
    $('[id*="uploaded_files_"]').on('shown.bs.modal', function (e) {
    $("#auditinstanceupload").html("<input id='audit_instance_id' name='audit_instance_id' value='"+questionID+"' type='hidden' style='display:none;'>");
    $("#auditidupload").html("<input id='audit_id' name='audit_id' value='"+questionID+"' type='hidden' style='display:none;'>");
    });
  });
});

<!-- 5 is a php row id for an example -->

 <a title="Upload file" href="#" id="5"
class="btn btn-default file_to_upload" >upload button</a>


<!-- modal pop up for [F] button -->
<div class='modal fade' style='z-index:10000' id='uploaded_files_5' role='dialog'>
  <div class='modal-dialog modal-lg'>
    <div class='modal-content'>
    <div class='modal-header'>
    <button type='button' class='close' data-dismiss='modal' aria-hidden='true'></button>
    <h4 class='modal-title'>Perform Audit</h4>
  </div>
  <div class='modal-body' id='perform_audit1'>
    <form id='my_form' name='form' action='ajax/file_upload.php' method='POST' enctype='multipart/form-data' style=''>
      <h1>Upload File</h1>
      <div id='main'>
        <strong>File: </strong><input name='myFile' id='myFile' size='27' type='file'>
        <input id='my_button' name='action' value='Upload' onclick='uploadshow()' type='button'>
        <input id='auditinstanceupload' style='display:none;' />
        <input id='auditidupload' style='display:none;' />
        <input id='question_id' name='question_id' value='$row[questionID]' type='hidden'>
      </div>
      <!--<input id='close_file_upload' value='Close' type='button'>-->
    </form>
  </div>
  <div class='modal-footer'>
    <form method = 'POST'>
      <input type='button' id='yes_delete' value='Yes ' name='view_audits_delete' />
      <button type='button' class='btn btn-default' data-dismiss='modal'>No</button>
       </form>  
      </div>
    </div>
  </div>
</div>
<!-- modal pop up for [F] file upload button -->            

3 个答案:

答案 0 :(得分:2)

click()主要用于在bootstrap模式完全打开时执行函数。

如果要在单击按钮时执行功能,则不需要此方法。这是出于不同的目的。您可以为此目的使用jQuery $( "#modalButtonId" ).click(function() { alert( "Handler for .click() called." ); //write other methods here }); 函数:

UNION

source

答案 1 :(得分:0)

试试这个

$(document).ready(function(){
  $(".file_to_upload").click(function (data) {
    //alert("works");
        //var questionID = data.id;
        var questionID = $(this).attr("id");
        alert(questionID);
    $('[id*="uploaded_files_"]').modal('show');
    $('[id*="uploaded_files_"]').on('shown.bs.modal', function (e) {
      $("#auditinstanceupload").html("<input id='audit_instance_id' name='audit_instance_id' value='"+questionID+"' type='hidden' style='display:none;'>");
      $("#auditidupload").html("<input id='audit_id' name='audit_id' value='"+questionID+"' type='hidden' style='display:none;'>");
    });
  });
});

答案 2 :(得分:0)

var questionID = $(this)[0] .id

这对我有用!