0

我想将表单名称传递给函数,然后确认它们已被读取以删除它,然后我想更改操作然后提交表单。

function deletePhone(myForm){
   var r=confirm('Are you sure you want to delete this phone?');
   if(r==true){
       $(myForm).attr("action","index.cfm?deleteThis=1");
       $(myForm).submit();
   }
}

currentrow 是我正在使用的一个变量,因为我正在创建多个表单,并且它保存表单的 currentCount。

   <a href="javascript:deletePhone('EditPhone_form_#currentrow#');">X Delete Phone</a>

例子:

  • EditPhone_form_1
  • EditPhone_form_2
  • EditPhone_form_3
  • EditPhone_form_4
4

3 回答 3

1

似乎当您根据 ID 进行 jQuery 查找时,它的格式不正确。

尝试这个:

   function deletePhone(myForm){
       var r=confirm('Are you sure you want to delete this phone?');
       if(r==true){
          $('#'+myForm).attr("action","index.cfm?deleteThis=1");
          $('#'+myForm).submit();
       }
    }

注意额外添加的#

编辑:您还没有发布表单的整个 HTML,但大概标签在表单本身中,对吧?如果是,那么你也可以试试这个:

<a href="javascript:deletePhone($(this));">X Delete Phone</a>

   function deletePhone(link){
       var r=confirm('Are you sure you want to delete this phone?');
       if(r==true){
          var form = link.closest('form');
          form.attr("action","index.cfm?deleteThis=1");
          form.submit();
       }
    }
于 2012-09-20T12:44:14.373 回答
0

您可以在表单中添加隐藏字段,而不是更改操作。如果是表单名称,您应该使用$("[name='"+myForm+"']")to 元素。myForm

$("[name='"+myForm+"']").append("<input type='hidden' value=1 name=deleteThis />");

所以你的代码将是。

 function deletePhone(myForm){
       var r=confirm('Are you sure you want to delete this phone?');
       if(r==true){
          $("[name='"+myForm+"']").append("<input type='hidden' value=1 name=deleteThis />");
          $("[name='"+myForm+"']").submit();
       }
    }
于 2012-09-20T12:41:56.240 回答
0

我宁愿看到您使用 jQuery 事件绑定而不是使用 onclick 事件:

function deletePhone(e){
    e.preventDefault();
    var r = confirm('Are you sure you want to delete this phone?');
    if(r){
        $(this)
            .closest('form')
            .attr("action","index.cfm?deleteThis=1")
            .submit();
    }
}

$(".remove-phone").on("click", deletePhone);

并使用如下标记:

<a href="#" class="remove-phone">X Delete Phone</a>
于 2012-09-20T14:02:31.817 回答