Javascript 如何在模式弹出窗口中获取标记的值

Javascript 如何在模式弹出窗口中获取标记的值,javascript,jquery,ajax,Javascript,Jquery,Ajax,我有下面的代码,我想要一个标记的值 <div class="modal fade show" id="myModal" tabindex="-1" role="dialog" aria- labelledby="myModalLabel" style="display: block;"> <div class="modal-dialog" role="document"> <div class="

我有下面的代码,我想要一个标记的值

  <div class="modal fade show" id="myModal" tabindex="-1" role="dialog" aria- 
    labelledby="myModalLabel" style="display: block;">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"> 
   <span aria-hidden="true">×</span></button>
                        <h4 class="modal-title" id="myModalLabel">Ingridiants  Detail</h4>
                    </div>
                    <div class="modal-body">
                        <div id="dvDetail">&nbsp;&nbsp;<table><tbody><tr><td>Name</td>
  <td>quantity</td></tr><tr><td>Chicken&nbsp;&nbsp;</td><td>1&nbsp;&nbsp;</td><td><a class="fa fa- 
  times" id="btnRemove" title="Delete" value="Chicken"></a>&nbsp;&nbsp;</td></tr></tbody></table> 
  </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data- 
   dismiss="modal">Close</button>
                    </div>
                </div>
            </div>
        </div>

警报未定义。但是所有其他代码都在工作。

有一些错误

  • 锚点
    没有
    属性,因此您可以如下使用数据
    attr
  • 你有错误发现它
  • $('body#dvDetail')。在('click',function(){
    var _t=$(本);
    如果(确认(“您确定要删除此附件”)){
    var tr=$(this.nexist('tr');
    var CartId=_t.find(#btnRemove”).data('value');
    警报(CartId);
    //删除下面发送的ID为的记录
    美元邮政(
    “/RegisterFoods/DeleteIng/”,
    {FoodName:CartId},
    职能(项目){
    tr.remove();
    }“json”);
    location.reload();
    }
    });
    
    
    ×
    细节
    名称
    数量Chicken 1
    接近
    
    问题:您试图找到最近的
    tr
    最近的
    tr
    意味着如果元素找到最近的元素,它将在该位置停止。因此,您需要遍历所有出现的
    tr
    ,而不是找到
    #btnRemove

    尝试下面的代码,希望它能帮助你

    $('body#dvDetail')。在('click',function()上{
    如果(确认(“您确定要删除此附件”)){
    var tr=$(this).find('tr#btnRemove');
    var CartId=tr.attr('value');
    警报(CartId);
    //删除下面发送的ID为的记录
    美元邮政(
    “/RegisterFoods/DeleteIng/”{
    食物名称:CartId
    },
    职能(项目){
    tr.remove();
    }“json”);
    location.reload();
    }
    });
    
    
    ×
    细节
    名称
    量
    鸡
    1.
    接近
    
    只需在
    find()
    中使用
    #btnRemove
    ,或者更好地使用类
    .btnRemove
    而不是id
        $('body #dvDetail').on('click', function () {
            if (confirm("Are you sure to delete this ingridients")) {
                var tr = $(this).closest('tr');
                var CartId = $(this).closest('tr').find("btnRemove").val();
                alert(CartId);
                //Deletes the record with ID sent below
                $.post(
                    '/RegisterFoods/DeleteIng/',
                    { FoodName: CartId },
                    function (item) {
                        tr.remove();
                    }, "json");
    
                location.reload();
    
            }
        });