javascript JScript 错误:无法分配给函数结果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12267411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 15:45:37  来源:igfitidea点击:

JScript error: Cannot assign to a function result

javascriptjqueryasp.net

提问by Henry

In my application I am trying to hide a table based on a condition. I am using simple javascript function to hide the table.

在我的应用程序中,我试图根据条件隐藏表格。我正在使用简单的 javascript 函数来隐藏表格。

the function is failing on the following line

该功能在以下行失败

if ((image1.trim() = '') && (image2 != '1')) {

giving error

给出错误

'Microsoft JScript runtime error: Cannot assign to a function result'.

“Microsoft JScript 运行时错误:无法分配给函数结果”。

Here is the code.

这是代码。

Html code:

html代码:

 <table id="tblImage" cellpadding="0" cellspacing="0" style="padding:2px 0px">
            <tr>                
                <td>
                    <div id="otherImages"></div>
                </td>
            </tr>
     </table>   

Javascript function:

Javascript函数:

function DisplayTable() {
    var image1 = document.getElementById('ctl00_ContentPlaceHolder1_image1').value;
    var image2 = document.getElementById('ctl00_ContentPlaceHolder1_image2').value;
    if ((image1.trim() = '') && (image2 != '')) {
         jQuery('#tblImage').hide();
    }
}

回答by Zbigniew

You are using =, but you should use ==:

您正在使用=,但您应该使用==

if ((image1.trim() == '') && (image2 != '1')) {

Basicaly =means assign value and ==means is equal to. This generates error because it is not possible to assign value to function (which happens where, you're trying to assign value to trim()).

基本上=意味着赋值并且==意味着等于。这会产生错误,因为无法为函数赋值(这发生在您尝试为 赋值的地方trim())。

回答by Jo?o Silva

You are using =(assignment) instead of ==(equals) in the folowing ifstatement, resulting in the assignment to a functionerror. Use the following instead:

您在以下语句中使用=( assignment) 而不是==( equals) if,从而导致assignment to a function错误。请改用以下内容:

if ((image1.trim() == '') && (image2 != '')) {
     jQuery('#tblImage').hide();
}

回答by Shmiddty

if ((image1.trim() = '') && (image2 != '')) {

if ((image1.trim() = '') && (image2 != '')) {

should be

应该

if ((image1.trim() == '') && (image2 != '')) {
              this__^