Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用JavaScript字符串创建'<;%=。。。ClientID%>';动态的?_Javascript_Asp.net_String - Fatal编程技术网

如何使用JavaScript字符串创建'<;%=。。。ClientID%>';动态的?

如何使用JavaScript字符串创建'<;%=。。。ClientID%>';动态的?,javascript,asp.net,string,Javascript,Asp.net,String,您好,我正在尝试编写一个javascript生成,其中元素是函数的输入参数之一。我正在尝试写一个变量 var elementName = "<%=" + element + ".clientID%>" var elementName=“” 这是一个错误 CS0117:“字符串”不包含 “clientID”的定义 这会引发一个错误,因为标记的服务器端处理首先发生。然后,结果输出为文本,在本例中为JavaScript。因此,服务器看到这个并抛出一个错误,因为它无法理解。它假设字符串

您好,我正在尝试编写一个javascript生成
,其中元素是函数的输入参数之一。我正在尝试写一个变量

var elementName = "<%=" + element + ".clientID%>" 
var elementName=“”
这是一个错误


CS0117:“字符串”不包含 “clientID”的定义


这会引发一个错误,因为标记的服务器端处理首先发生。然后,结果输出为文本,在本例中为JavaScript。因此,服务器看到这个
并抛出一个错误,因为它无法理解。它假设字符串
“+element+”
是调用了
.clientID
的值

有两种方法可以解决这个问题

选项1:输出所有ID

对元素的所有可能值进行散列。如果你有不止一对夫妇,这种方法可能不适合你

var elements = [
    coolbutton: '<%= coolbutton.ClientId %>',
    simplebutton: '<%= simplebutton.ClientId %>',
    otherbutton: '<%= otherbutton.ClientId %>'
];
选项2:使用带结尾的

如果您可以使用jQuery,那么有一种方便的方法可以通过ID引用ASP.Net控件。实际上,所有ASP.Net ID都以您在代码中使用的实际变量名结尾。因此,如果您有一个名为CoolButton的元素,您可以使用jQuery在javascript中找到它,如下所示:

var element = $('[id$=CoolButton]');
$=运算符表示以结尾。因此,它会查找页面上ID以CoolButton结尾的所有元素。页面上ASP.Net控件的实际ID会很长,但它总是以
NamingContainer$CoolButton
结尾

如果不能使用jQuery,可以自己编写这个逻辑

function findByIdEndsWith(endId) {
    //loop through all elements on the page
        //if the current element's id ends with endId, return it

    //return null;
}

这种类型的函数将被详细讨论。

正如Edmassa指出的,您的服务器端处理正在评估(可能是ERB?)


试着逃避你的错误是什么?它会产生什么错误?你会得到什么错误?(遵循趋势)CS0117:“字符串”不包含“clientID”的定义现在该错误出现在哪里?:)那将在一分半钟内解开谜团。(见附件)
function findByIdEndsWith(endId) {
    //loop through all elements on the page
        //if the current element's id ends with endId, return it

    //return null;
}