Javascript:使用变量访问服务器端变量客户端

发布于 2024-12-03 03:20:02 字数 1504 浏览 3 评论 0原文

我正在尝试:

  1. 使用 JQuery “循环”页面上属于同一 CSS 类(“样板”)的所有元素
  2. 根据其服务器端分配的值检查每个元素的当前值(属性:StaticPrefill)
  3. 应用特殊的 css class ("editedbackcolor") 如果两个值不匹配(即我试图在有人编辑文本框上的预填充文本时进行标记)

我正在使用的CSS:

.boilerplate =分配给所有文本框我试图检查表单

.editedbackcolor = 不同的阴影我想分配给当前值不等于服务器端 StaticPrefill 值的文本框。

我到目前为止的 jQuery 代码是:

jQuery(document).ready(function () {

        // select each element with class boilerplate and run a function against it
        jQuery('.boilerplate input').each(function () {              
            var target1 = jQuery(this).attr("id");
            matchcheck(target1);
        });
    });

我正在研究“matchcheck) 函数,这是我遇到问题的地方。我正在尝试拉回服务器端“StaticPrefill”属性值,我可以将其用作比较,我已经通过硬编码控件名称成功查询了此值,例如:

function matchcheck(){         
                var TSP1 = '<%= TextBox1.StaticPrefill %>';
                // If current textbox value does NOT equal it's static prefill value
                if (document.getElementById("Textbox1_textbox1").value != TSP1) {
                alert("TB1 has differnt value than static prefill");
                // change background color to flag it
                jQuery("#Textbox1_textbox1").addClass("EditedBackColor");                   
                }
            }

效果很好,但我不想使用变量来循环。所有元素而不是硬编码函数第一行中的“TextBox1”我尝试了不同的语法,试图在 '<%= ' 和 '%>' 之间放置一个变量。标签,但当我尝试此操作时,页面无法编译。

是否可以使用某种隐藏代码?

I am attempting to:

  1. Use JQuery to 'loop' through all elements on a page that belong to the same CSS class ("boilerplate")
  2. Check the current value of each against it's server side assigned value (property: StaticPrefill)
  3. Apply a special css class ("editedbackcolor") if the two values do not match (ie I'm trying to flag when someone has edited the prefilled text on textboxes)

CSS I am using:

.boilerplate = assign to all text boxes I'm trying to check on the form

.editedbackcolor = different shade I want to assign to textboxes where current value does NOT equal server side StaticPrefill value.

jQuery code I have so far is:

jQuery(document).ready(function () {

        // select each element with class boilerplate and run a function against it
        jQuery('.boilerplate input').each(function () {              
            var target1 = jQuery(this).attr("id");
            matchcheck(target1);
        });
    });

and I'm working on the "matchcheck) function which is where I am having a problem. I'm trying to pull back the server side "StaticPrefill" property value that I can use as a comparison. I've successfully queried this by hardcoding a control name, like:

function matchcheck(){         
                var TSP1 = '<%= TextBox1.StaticPrefill %>';
                // If current textbox value does NOT equal it's static prefill value
                if (document.getElementById("Textbox1_textbox1").value != TSP1) {
                alert("TB1 has differnt value than static prefill");
                // change background color to flag it
                jQuery("#Textbox1_textbox1").addClass("EditedBackColor");                   
                }
            }

That works fine, but I don't want to use a variable to loop through all elements instead of the hardcoded "TextBox1" in the first line of the function. I've tried different syntaxes in an attempt to put a variable between the '<%= ' and '%>' tags but the page won't compile when I try this.

Is this possible w/o using code behind of some sort? Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

递刀给你 2024-12-10 03:20:02

如果我理解你的问题,一个更简单的方法是绑定到 jQuery 中的 change 事件

 $(document).ready(function() {
   jQuery('.boilerplate input').change(function() {
       if(jQuery(this).val() !== jQuery(this).attr("prefill")) {
        jQuery(this)removeClass('boilerplate').addClass("EditedBackColor");
       }
    });
});

您还需要向输入元素添加一个名为“prefill”的属性。您可以在服务器端或客户端执行此操作。它应该看起来像这样:

<input class="boilerplate" id="input1" type="text" prefill="123" value="123"/>

这里需要注意的是,如果他们将其更改回原始值,它仍然会显示为已更改。我不确定这是否适合您的情况。他们确实改变了它本身。如果原始值不存在,则必须将其保存为属性。另一种选择是发送一个模型,可能是一个 json 对象,并根据索引与该模型进行比较。

An easier approach, if I understand your question, is to bind to the change event in jQuery.

 $(document).ready(function() {
   jQuery('.boilerplate input').change(function() {
       if(jQuery(this).val() !== jQuery(this).attr("prefill")) {
        jQuery(this)removeClass('boilerplate').addClass("EditedBackColor");
       }
    });
});

You will also need to add an attribute called "prefill" to your input elements. You can do that server side or client side. It should look something like this:

<input class="boilerplate" id="input1" type="text" prefill="123" value="123"/>

The caveat here is that if they change it back to the original value, it will still show as changed. I'm not sure if that works for you in your scenario. They did change it per se. You would have to save off the original value as an attribute if it did not exist. The other option is to send down a model, maybe a json object, and compare to that model based on index.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文