我有一个动态生成的表单。表单内部是一个执行AJAX请求的函数,并且在成功时,修改用于验证的隐藏字段的值(将其设置为空字符串)。(隐藏字段设置为“required:true”)
但是,成功执行该函数后,隐藏字段不会触发jQuery的验证,我可以提交表单。隐藏字段的验证仅在我有意触发其他字段的验证时触发,并且将显示隐藏字段的错误消息以及其他错误消息。
功能:
function DeleteImageDP(){
var itemid=$('#DisplayDeleteItemID').val();
var filepath=$('#DisplayDeleteFilePath').val();
var itempicid=$('#DisplayDeleteItemPicID').val();
var cfm=confirm("Confirm deletion of picture? ( Note: Picture wil be deleted permanently.");
if(cfm == true)
{
$.ajax({
url:"delete/deletedp.php",
type:"POST",
data:"ItemID="+itemid+"&FilePath="+filepath+"&ItemPicID="+itempicid,
success:function(){
alert("Image successfully deleted.");
$('#ImagePreviewDP').prop('src','').hide();
$('#ImagePreviewDPValidate').val('');
$('#DisplayDelete').hide();
},
error:function(){
alert("Image could not be deleted due to an error.");
}
});
return true;
}
else
{
return false;
}
};
验证:
$('#ItemDetailsContainer').on('change',function(){
//Validation code .....
ImagePreviewDP:{
required:true
},
//More validation code....
});
表(提取物):
//Input and preview for Display Pic
echo"<div class='BizEditItemDetails' >";
//More code above
echo"<img id=ImagePreviewDP name=ImagePreviewDP class='ImagePreview' src=\"$dp\" > ";
echo"<input type='hidden' id='ImagePreviewDPValidate' name='ImagePreviewDPValidate' value=\"$dp\" >";
//More code below
echo"</div>";
为什么不将$(“#ImagePreviewDP”)的值设置为空字符串触发on.change?另外,如果成功执行该函数,我将如何获得验证,而不是仅与其一起触发其他错误?
答案 0 :(得分:2)
使用javascript以编程方式设置元素值永远不会触发onchange
事件,您必须自己触发
$.ajax({
url:"delete/deletedp.php",
type:"POST",
data:"ItemID="+itemid+"&FilePath="+filepath+"&ItemPicID="+itempicid,
success:function(){
alert("Image successfully deleted.");
$('#ImagePreviewDP').prop('src','').hide();
$('#ImagePreviewDPValidate').val('');
$('#DisplayDelete').hide();
$('#ItemDetailsContainer').trigger('change');
// ^^ trigger event handler
},
...........