2014-03-03 96 views
-1

我需要通过JavaScript确认对话框进行简单表单提交。 提交将删除数据库中的项目,所以我想通过打印有关要删除的元素的信息来询问确认消息。将参数传递给JavaScript函数以提交表单

形式的代码是:

echo "<form action='manage_cycles.php' method='POST' onsubmit='return conferma($(this))' name='form_form'>"; 
    echo "<input type='hidden' name='cycle_id' value='".$cycle['cycleID']."'>"; 
    echo "<td style='width:40px;'><button type='submit' name='delete_cycle' value='".$cycle['cycleName']."' style='color:blue;font-size:12px'>Delete</button></td>"; 
echo "</form>"; 

我想要在确认对话框来打印的信息是按钮标签,$周期[“循环名”]的值。

JavaScript函数的代码是:

function conferma(element){ 

    var button_value = element.find("button").attr("value"); 

    var msg = "Are you sure you want to delete " + button_value + "?"; 

    return confirm(msg); 
    } 

的确认对话框消息是:

是否确定要删除未定义?

然后返回值正常工作,并通过单击Y/N选项提交或不提交表单。

我不明白的地方,我错了

+0

'return conferma(this)'而不是返回'conferma($(this))'并用'$(element)'包装'element'使'.find()'工作! –

+0

尝试使用return conferma(this)和包装元素与$(元素)...不起作用(我想我试过所有组合用于传递参数和wrap元素) –

+0

你试过'element.find(“button”) .VAL()'? –

回答

1

试试这个 “coglione”

+1

欢迎来到Stack Overflow!虽然这可能会回答这个问题,但如果你能解释一下如何以及为什么回答这个问题,那会更好。 – apaul

0

我想你想

conferma(this) 

,而不是

conferma($(this)) 
0

如果您正在使用jQuery,试试这个:

$('[name=form_form]').submit(function() { 
    var btn_val = $(this).find('button').attr('value'); 
    return confirm("Are you sure, you want to delete " + btn_val + "?"); 
}); 

and omit onsubmit - 在您的form -tag上发挥作用。

+0

我试过类似的东西(用js启动函数按下type =“button”按钮,否则提交),但我不知道为什么变量$ _POST ['delete_cycle']是没有设置,所以我无法通过PHP控制实际从数据库中删除。 –

0

快到了,试试这个方法:

表单元素:

onsubmit='return conferma(this)' 

功能:

function conferma(element){ 

    var button_value = $(element).find("button").attr("value"); 

    var msg = "Are you sure you want to delete " + button_value + "?"; 

    return confirm(msg); 
    } 
0

调用此方法

function conferma(){ 

    var button_value = $("button[name='delete_cycle']").val(); 
    return confirm("Are you sure you want to delete " + button_value + "?"); 

}