一页上有多个表单模态

时间:2017-01-03 20:13:58

标签: javascript jquery twitter-bootstrap twitter-bootstrap-3 bootstrap-modal

我需要有关javascript的帮助。我正在尝试使用多个表单在模式中显示动态名称。这就是我所拥有的,但它说“你确定要删除bob吗?”但如果我点击删除汤姆按钮,我希望它显示汤姆。

表格:

<form role="form" id="formfield" action="<?=$_SERVER['PHP_SELF']?>" method="post">
<input type="hidden" name="username" id="username" value="bob" /> 
<input type="hidden" name="delete_id" id="delete_id" value="45" /> 
<button type="button" name="btn" value="Delete" id="submitBtn" data-toggle="modal" data-target="#confirm-delete" class="btn btn-xs btn-danger tooltip-error no-border" style="padding:5px; width:30px; height:30px;" /><i class="fa fa-trash-o bigger-120"></i></button>
</form>

<form role="form" id="formfield" action="<?=$_SERVER['PHP_SELF']?>" method="post">
<input type="hidden" name="username" id="username" value="tom" /> 
<input type="hidden" name="delete_id" id="delete_id" value="48" /> 
<button type="button" name="btn" value="Delete" id="submitBtn" data-    toggle="modal" data-target="#confirm-delete" class="btn btn-xs btn-danger tooltip-error no-border" style="padding:5px; width:30px; height:30px;" /><i class="fa fa-trash-o bigger-120"></i></button>
</form>

模态:

<div class="modal fade" id="confirm-delete" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                Confirm Submit
            </div>
            <div class="modal-body">
                Are you sure you want to delete <span id="deletename_id">?
            </div>
            <div class="modal-footer">
                <button type="button" id="cancel" class="btn btn-default success" data-dismiss="modal">Cancel</button>
                <a href="#" id="delete" class="btn btn-danger success">Delete</a>
            </div>
        </div>
    </div>
</div>

Javascript:

<script>
$('#submitBtn').click(function() {
     $('#deletename_id').text($('#username').val());
});

$('#delete').click(function(){
    $('#formfield').submit();
});
</script>

2 个答案:

答案 0 :(得分:1)

首先,您需要使用submitBtn类,因为您有两个具有相同id的元素。 请试试这个:

$('.submitBtn').click(function() {
    $('#deletename_id').text($(this).parents('form').find('input').eq(0).val());
});

另一种方法是使用此方法:

$('#deletename_id').text($(this).parents('form').find('#username').val());

在这两种方法中,您需要找到包含form点击的button,然后您需要输入find

$('.submitBtn').click(function() {           
  $('#deletename_id').text($(this).parents('form').find('input').eq(0).val());
});

$('#delete').click(function(){
    $('#formfield').submit();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<form role="form" id="formfield" action="<?=$_SERVER['PHP_SELF']?>" method="post">
<input type="hidden" name="username" id="username" value="bob" /> 
<input type="hidden" name="delete_id" id="delete_id" value="45" /> 
<button type="button" name="btn" class="submitBtn" data-toggle="modal" data-target="#confirm-delete" class="btn btn-xs btn-danger tooltip-error no-border"><i class="fa fa-trash-o bigger-120"></i>Delete</button>
</form>

<form role="form" id="formfield" action="<?=$_SERVER['PHP_SELF']?>" method="post">
<input type="hidden" name="username" id="username" value="tom" /> 
<input type="hidden" name="delete_id" id="delete_id" value="48" /> 
<button type="button" name="btn" class="submitBtn" data-toggle="modal" data-target="#confirm-delete" class="btn btn-xs btn-danger tooltip-error no-border"><i class="fa fa-trash-o bigger-120"></i>Delete</button>
</form>
<div class="modal fade" id="confirm-delete" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                Confirm Submit
            </div>
            <div class="modal-body">
            Are you sure you want to delete <span id="deletename_id"></span>?
            </div>
            <div class="modal-footer">
                <button type="button" id="cancel" class="btn btn-default success" data-dismiss="modal">Cancel</button>
                <a href="#" id="delete" class="btn btn-danger success">Delete</a>
            </div>
        </div>
    </div>
</div>

答案 1 :(得分:0)

如果你想让javascript独一无二,那么你需要思考一点动态,比如以这样一种方式提供id,你可以在获取数据时只需更改一个字符就可以获得不同形式的不同字段数据。

我想说的是,正如您在我的代码中所看到的那样,我已经附加了&#39; _1&#39;和&#39; _2&#39;在您的ID&#39;用户名&#39;之后和&#39; delete_id&#39;。我也使用了一种生成bootstrap模式的高级方法。因此,您不需要自己编写孔代码。您可以从this link清楚了解此方法。

现在,请注意,单击按钮后,我调用了一个已传递id的方法。如果该ID不同,那么我可以在该ID上获得名称。

出于测试目的,复制过去任何表格并在&#39; _&#39;之后更改数字。在你喜欢的数字的每个id中,并在删除形式之后将其放在括号中#39;函数调用作为参数。

&#13;
&#13;
function deleteForm(formid) {
  var name = $('#username_' + formid).val();
  console.log(name);
  BootstrapDialog.show({
    title: 'Confirm Submit',
    message: 'Are you sure you want to delete '+name+' ?',
    buttons: [{
      label: 'Cancel',
      action: function(dialog) {
        dialog.close();
      }
    }, {
      label: 'Delete',
      cssClass : 'btn btn-danger',
      action: function(dialog) {
        // Do whatever you want to do here
        console.log('Do whatever you want to do here');
        dialog.close();
      }
    }]
  });

}
&#13;
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap3-dialog/1.34.7/css/bootstrap-dialog.min.css">

<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">


<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap3-dialog/1.34.7/js/bootstrap-dialog.min.js"></script>





<form role="form" id="formfield_1" action="" method="post">
  <input type="hidden" name="username" id="username_1" value="bob" />
  <input type="hidden" name="delete_id" id="delete_id_1" value="45" />
  <button type="button" name="btn" value="Delete" id="submitBtn_1" class="btn btn-xs btn-danger tooltip-error no-border" style="padding:5px; width:30px;height:30px;" onclick="deleteForm(1)" /><i class="fa fa-trash-o bigger-120"></i>
  </button>
</form>

<form role="form" id="formfield_2" action="" method="post">
  <input type="hidden" name="username" id="username_2" value="tom" />
  <input type="hidden" name="delete_id" id="delete_id_2" value="48" />
  <button type="button" name="btn" value="Delete" id="submitBtn_2" class="btn btn-xs btn-danger tooltip-error no-border" style="padding:5px; width:30px; height:30px;" onclick="deleteForm(2)" /><i class="fa fa-trash-o bigger-120"></i>
  </button>
</form>
&#13;
&#13;
&#13;