Can I use a confirm() prompt inside a function? How?
How do I cause the cancel button to not process the save on the page.
I am trying to only initiate the prompt when element id 'item' is changed.
I get the OK and CANCEL prompt very well indeed, but cancel does not stop the page from submitting.
<script type='text/javascript'>
var strProductNum = document.getElementById('item').value;
function isProductNumChanged(itemValue){
if (strProductNum != itemValue){
var valueYesNo = confirm('Really change this Product Numb开发者_JS百科er to ' + document.getElementById('item').value + '?');
}
if (valueYesNo){
return true;
}else{
return false;
}
}
</script>
This is how I fire the event:
<h:commandButton id="save1"
onclick="isProductNumChanged(document.getElementById('item').value);"
value="Save" action="#{itemHome.saveItem}" />
When using inline event handlers (like onclick=""
) and you need to stop the default event happening, it is not enough to return false from your event handler function. You also have to put return
before the name of the function when you specify the inline event handler.
onclick="return isProductNumChanged(document.getElementById('item').value);"
精彩评论