[英]passing old value back to javabean from javascript
I have this piece of javascript: 我有一段JavaScript:
<script type="text/javascript">
function show_confirm()
{
var type = '<%= nameBean.getTxnType() %>';
var old_cd = '<%= nameBean.getCode() %>';
var new_cd = document.getElementById("tbCode").value;
var cd;
if (type == "Update")
{
if(old_cd != new_cd)
{
var response = confirm("Code already exists. Do you want to replace it?");
if (response){
document.NameUpdate.submit();
}
else{
cd = old_cd;
}
}
</script>
and this is what i am doing in my jsp page to invoke this script: 这就是我在我的jsp页面中执行的操作以调用此脚本:
<INPUT TYPE=SUBMIT NAME="action" onclick="show_confirm()" VALUE="Save Changes">
Its working fine when I hit ok.. but my question is how can i pass the value of old_cd back to the bean so it wont update it with the new code that was entered by the user in the tbcode box.. when user hit cancel i want to ignore what value was entered in textbox and not to update that field in database 当我点击确定时,它的工作正常..但是我的问题是我如何将old_cd的值传递回Bean,以便它不会使用用户在tbcode框中输入的新代码来更新它。我想忽略在文本框中输入的值,而不是更新数据库中的该字段
I'm not entirely clear on the use case here, but here are a couple of answers: 我对这里的用例不完全清楚,但是有两个答案:
If the question is, "how do I stop the form from submitting when the user hits cancel?", then the answer is, return false
in the click handler: 如果问题是“当用户单击取消时如何停止提交表单?”,那么答案是,在点击处理程序中返回
false
:
if (response){
document.NameUpdate.submit();
}
else{
cd = old_cd;
return false;
}
If you need to submit the form no matter which one the user clicks, then you probably need to submit the old value in a hidden input
field and have a way to tell the server that user hit "cancel" (probably another hidden field), eg: 如果无论用户单击哪个表单都需要提交表单,那么您可能需要在一个隐藏的
input
字段中提交旧值,并有一种方法可以告诉服务器用户该用户点击了“取消”(可能是另一个隐藏字段),例如:
<!-- html -->
<input type="hidden" name="old_cd" value="<%= nameBean.getCode() %>">
<input type="hidden" id="canceled" name="canceled" value="0">
and javascript: 和javascript:
// js snippet
if (response){
document.NameUpdate.submit();
}
else{
document.getElementById("canceled").value = 1;
return true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.