我有一个按钮,使用在外部文件中定义的功能为我的网站提交付款。我想添加一个警告框弹出窗口,用于确认或取消使用按钮onclick
调用的功能。我熟悉javascript,但是,如果它在外部定义,我不确定如何在另一个函数内调用该函数。
我有什么:
<script type="text/javascript">
var $jj = jQuery.noConflict();
$jj(document).ready(function () {
$jj('.alertbox').on('click', function () {
var _this = $jj(this);
$jj.confirm({
title: 'Confirm!',
content: 'Are you sure?',
buttons: {
confirm: function review.save(); {
},
cancel: function () {
}
}
});
});
});
</script>
按钮phtml
:
<button type="submit" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Place Order')) ?>" class="button btn-checkout alertbox" onclick="review.save();" ><span><span><?php echo $this->__('Place Order') ?></span></span></button>
我知道这不起作用,因为我收到以下错误:
Uncaught ReferenceError: review is not defined
at HTMLButtonElement.onclick
我摆脱了onclick
吗?
有更有效的方法吗?也许使用form
和input
而不是button
?
答案 0 :(得分:0)
$('#press').on('click', function(){
var val = validate();
if(val == true){
var r = confirm("Submit form!")
var txt;
if (r == true) {
txt = "You pressed OK!";
//$( "#myform" ).submit(); //use this for submitting the form
} else {
txt = "You pressed Cancel!";
}
alert(txt); //this line of code for test reasons
}
else{
alert("input fields empty");
}
});
function validate(){
var val = true;
var teste1 = $("#input1").val();
var teste2 = $("#input2").val();
if(teste1== "" || teste1 == null){
var val = false;
//some css and jquery to change the input color
}
if(teste2 == "" || teste2 == null){
var val = false;
//some css and jquery to change the input color
}
return val;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id = "myform" action="test.php" method="post">
<input type = "text" id ="input1" value = "">
<input type = "text" id ="input2" value = "">
<button id = "press" type="button">Click Me!</button>
</form>
&#13;