简体   繁体   English

加载页面时检查文本框的默认值更改

[英]check textboxes for change in value from default value when page loaded

I have a two forms on a page. 我在页面上有两种形式。

The first one has several quantity text boxes that are populated with their respective default values on page load, which can obviously change. 第一个有几个数量文本框,这些文本框在页面加载时填充了各自的默认值,这些默认值显然可以更改。 Also there may be 1, 2 ,3 or more of these text boxes on the page, so a script would have to account for this. 页面上也可能有1、2、3个或更多这些文本框,因此脚本必须对此进行说明。 Each text box will have a sequential name of "Quantity1, Quantity2, etc" and the id will be the same as well. 每个文本框将具有一个顺序名称“ Quantity1,Quantity2等”,并且ID也将相同。

What I want to do it check to see if any of these have been changed from their default value and when either button is clicked on in the second form, it is to do the first form's button not the second form's button action. 我要做的是检查是否已更改其默认值,并且在第二个表单中单击任一按钮时,是执行第一个表单的按钮而不是第二个表单的按钮动作。

Now if there were no changes in any of the input text boxes, then the second form's buttons can and should act as normal. 现在,如果任何输入文本框中都没有更改,则第二个表单的按钮可以并且应该像平常一样工作。 Is this possible and if so a jquery method is preferred? 这可能吗,如果可以,则首选jquery方法?

Note: I stripped out a lot of unnecessary code here so only the relevant stuff is shown. 注意:我在这里删除了很多不必要的代码,因此仅显示相关内容。

<form method="POST" name="form" action="ShoppingCart.asp" onsubmit="if (typeof(Update_Hidden_State_Fields)=='function') Update_Hidden_State_Fields(); if (this.submitted) return false; else {this.submitted=true; return true;}">
<td><center><input type="text" maxlength="7" size="5" value="5" name="Quantity1" id="Quantity1"><center></td>
<td><center><input type="text" maxlength="7" size="5" value="2" name="Quantity2" id="Quantity2"><center></td> 
<td><center><input type="text" maxlength="7" size="5" value="5" name="Quantity3" id="Quantity3"><center></td>
<td width="135" align="right"><input border="0" type="image" id="btnRecalculate" name="btnRecalculate" src="v/vspfiles/templates/100/images/buttons/btn_recalculate.gif" alt="Recalculate Totals"></td>

Here is the form that when either button inside it is clicked on and any of the input boxes in the above forms have changed to do the first forms button action. 这是当单击其中的任何一个按钮并且上面的表单中的任何输入框都已更改以执行第一个表单按钮动作时所用的表单。

<form name="Proceed_To_Checkout_Form" method="post" action="https://www.dothisurl.com/login.asp">
<td><input type="image" src="v/vspfiles/templates/100/images/buttons/btn_checkout_guest.gif" name="btn_checkout_guest"></td>
<td><input type="image" src="v/vspfiles/templates/100/images/buttons/btn_checkout_guest.gif" name="btn_checkout_guest"></td>

I'll make a try : 我会尝试:

$(function(){
  $("[id^='Quantity']").each(function(){
    $(this).data('default', $(this).val());
  });

  $("[name='Proceed_To_Checkout_Form']").submit(function(){
    var hasChanged = false;
    $("[id^='Quantity']").each(function(){
      if($(this).val() != $(this).data('default')){
        hasChanged = true;
      }
    });
    if(hasChanged){
      $("#btnRecalculate").click();
      return false;
    }
  });
});

On page load, you could store the current values of the fields (these would be the default values, I assume) into a local js variable - most likely an array, to handle the fact that there might be 1, 2, or 3 values to be stored. 在页面加载时,您可以将字段的当前值(我认为是默认值)存储到本地js变量中-很可能是数组,以处理可能存在1个,2个或3个值的事实要存储。 Once you have those default values stored, the rest should be relatively easy: 一旦存储了这些默认值,其余的就应该相对容易了:

When a button is clicked on Form2, its implementation compares the relevant field's current value to its default value (the one you stored when the page loaded). 单击Form2上的一个按钮时,其实现会将相关字段的当前值与其默认值(页面加载时存储的默认值)进行比较。 If they're the same, do your normal processing. 如果它们相同,请进行正常处理。 If they're different, do the "exception case" processing you need. 如果它们不同,请执行所需的“例外情况”处理。

Hopefully I understood your question correctly, and this approach gets you on the right track. 希望我能正确理解您的问题,并且这种方法可以使您走上正确的道路。 I'll keep an eye on this thread, if you need me to throw together some code examples/snippets. 如果您需要我整理一些代码示例/代码片段,我将密切关注该线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM