2014-09-12 52 views
-1

我处于使用供应商提供的系统的情况,该系统内置了无法验证的脚本改性。Javascript修改现有页内脚本中无法手动更改的变量

页面上的其中一个脚本调用与文本框中的字符数相关的验证方法。

它将字符数限制作为方法调用中的静态数字传递。静态编号来自一个配置文件,我可以改变它。问题是我需要根据用户从下拉菜单中选择的值来更改此号码。

发生了什么是我必须在窗体提交文本框中添加内容。我附加的内容包括一个具有可变长度的下拉列表中的值。

我正在运行的脚本在文本框中追加内容的工作正常。我碰到的问题是用户输入的文本数量最多(有一个字符倒计时),当他们点击提交时,系统会从下拉菜单中添加内容,但会截断用户消息的结尾,因为它是现在比接受的数字更长。

由于这是一个供应商/封闭系统,我调整现有的页面脚本,表单元素等的选项非常有限。我可以添加脚本(包括jQuery脚本)来修改DOM。

任何想法如何处理这样的事情将非常感激。

谢谢!

编辑

这是不是一个工作的例子,但也许说明了什么我想要做的事:

<select name="selectedname" id="selectedname"> 
    <option value="1">Value 1</option> 
    <option value="2">Value 2</option> 
</select> 
<textarea id="message"></textarea> 
<span id="errormessage"><!-- this is where you would see the text counting down --></span> 
<script> 
    vendor_script_that_counts_down_text_and_validates_length(arg1, arg2, 500) 
</script> 
<button id="send"></button> 

我的脚本:

/* Attempt to add the name to the question*/ 
if (window.jQuery) { 
    //Adding this to prevent conflicts with existing javascript 
    jQuery.noConflict(); 
    // Adding usual jQuery symbol to the local scope of this method 
    jQuery(document).ready(function($){ 
     $("#send").on('click', function(e) { 
     var valueSelected = getName($); // this is a function that gets the value of the dropdown 
     var message = "This message is for " + valueSelected + ". The message begins here:\n\n"; 
     $("#message").val(function(index, userMessage){ 
      return message + userMessage; 
     }) 
     }); 
    }); 
} 

在怎样的条件脚本目前正在一起工作,他们不是,我想这是我问题的关键,他们可以一起工作吗?有没有一种方法可以修改我的脚本,以根据通过约束选择的下拉值更改验证脚本调用(500)中使用的数字,我无法修改除创建的脚本之外的任何HTML或页面脚本?

+2

[添加一些代码。也许是一个简化但工作的例子。](https://stackoverflow.com/help/mcve) – Yoshi 2014-09-12 15:49:09

+0

我可以添加一些代表正在发生的事情,但我无法添加供应商提供的将复制的验证脚本功能。这仍然有帮助吗? – someoneinomaha 2014-09-12 16:03:18

+0

好吧,在某种程度上,你必须向我们展示你的脚本如何一起工作。事实上,这是抽象的。 – Yoshi 2014-09-12 16:36:50

回答

1

从您添加的内容看来,供应商函数似乎是全局函数。如果是这样的话,你可以用一个新的覆盖它,给你改变/操纵第三个参数的可能性。

例如: - (供应商脚本之后添加此加载,但在此之前的函数来获取的称呼):

vendor_script_that_counts_down_text_and_validates_length = function (orgFunc) { 
    // return a new function 
    return function (arg1, arg2, charCount) { 
    // add your code... 

    // call the original function 
    return orgFunc(arg1, arg2, charCount || 500); 
    }; 
}(vendor_script_that_counts_down_text_and_validates_length); 
+0

非常感谢 - 这工作! – someoneinomaha 2014-09-12 18:24:52