JavaScript函数中的恼人行为

JavaScript函数中的恼人行为,javascript,html,Javascript,Html,在一个网页中,我有两个不同的图库,其中有几个图像。每个图像都指定了一个按钮以便删除。我决定使用相同的JavaScript函数和一个参数来区分两个库。这些是画廊: <div class="column col_12 gallery"> {% for testrigimage in testrigimages %} <div id="testRigImage_{{testrigimage.id}}" class

在一个网页中,我有两个不同的图库,其中有几个图像。每个图像都指定了一个按钮以便删除。我决定使用相同的JavaScript函数和一个参数来区分两个库。这些是画廊:

        <div class="column col_12 gallery">
            {% for testrigimage in testrigimages %}
                <div id="testRigImage_{{testrigimage.id}}" class="image" align="center">
                    <a href="{{MEDIA_URL}}{{testrigimage.image}}"><img src="{{MEDIA_URL}}{{testrigimage.image}}" width="150" height="100" /></a>
                    <i id="deleteTestRigImage_{{testrigimage.id}}" class="icon-remove-sign icon-large" style="display:none;cursor:pointer;color:darkGrey;position:absolute;top:9px;left:129px;" onclick="javascript:deleteFile('{{testrigimage.id}}','0');"></i>
                    <br>
                    {{testrigimage.name}}
                </div>
            {% empty %}
                No images have been added.
            {% endfor %}
        </div>


    <div class="column col_12 gallery">
        {% for picture in pictures %}
            <div id="picture_{{picture.id}}" class="image" align="center">
                <a href="{{MEDIA_URL}}{{picture.image}}"><img src="{{MEDIA_URL}}{{picture.image}}" width="150" height="100" /></a>
                <i id="deletePicture_{{picture.id}}" class="icon-remove-sign icon-large" style="display:none;cursor:pointer;color:darkGrey;position:absolute;top:9px;left:129px;" onclick="javascript:deleteFile('{{picture.id}}','1');"></i>
                <br>
                {{picture.name}}
            </div>
        {% empty %}
            No pictures have been added.
        {% endfor %}
    </div>
问题是总是打印(警告)“0”!当我单击第一个库(带有“0”参数的库)的图像时,它会向0发出警报。当我点击第二个图库的图像时,它也会提醒“0”,尽管已经分配了“1”


为什么会有这种行为

如果(type=“0”),请在代码中查看这一行

要检查是否相等,需要是
if(type==“0”)
if(type==“0”)


使用
=
将分配值并对分配的值求值,而
=
==
将比较这两个值。

如果(type=“0”)请参见代码中的这一行

要检查是否相等,需要是
if(type==“0”)
if(type==“0”)


使用
=
将分配值并对分配的值求值,而
=
==
将比较这两个值。

如果(type=“0”)
应该是
如果(type==“0”)
甚至
如果(type==“0”)
。一个“等于”符号表示赋值,赋值总是返回true一个像
jshint
这样的工具会为您找到它。我仍然时不时地输入这个错误。正如torazaburo提到的,查看jshint。将允许您复制/粘贴或许多代码编辑器包含此功能。
if(type=“0”)
应该是
if(type==“0”)
甚至是
if(type==“0”)
。一个“等于”符号表示赋值,赋值总是返回true一个像
jshint
这样的工具会为您找到它。我仍然时不时地输入这个错误。正如torazaburo提到的,查看jshint。将允许您复制/粘贴或许多代码编辑器包含该功能。“值分配始终返回true”这是不正确的。Value assignment返回被分配的值。在
的情况下,如果(something=0
),则该语句将通过或返回true,它不会失败。请随意建议编辑,因为这正是该声明最初出现的方式。注意:如果你想要的话,我会编辑你的帖子。第一次编辑应被视为无效而拒绝。“值分配始终返回true”这是不正确的。Value assignment返回被分配的值。在
的情况下,如果(something=0
),则该语句将通过或返回true,它不会失败。请随意建议编辑,因为这正是该声明最初出现的方式。注意:如果你想要的话,我会编辑你的帖子。第一次编辑应被视为无效而拒绝。
function deleteFile(model_id, type){

    var x = confirm('You are about to delete this picture. Are you sure?')

    if(type="0"){
        alert(type)
        url = "/tests/testSetUp/testrig/" + model_id + "/delete/"
    }else{
        alert(type)
        url = "/tests/testSetUp/pictures/" + model_id + "/delete/"
    }

    if (x) {

         $.ajax({
             type: "POST",
             url : url,
             data: {'csrfmiddlewaretoken': '{{ csrf_token }}'},
             dataType: 'text',
             success : function (data, textStatus, request) {
             data = eval("(" + data + ")");
             if (data.success) {
                var n = noty({
                    text: 'Picture successfully removed.',
                    type: 'success',
                    timeout: 750,
                    callback:{
                        afterClose: function(){location.reload()}
                    }
                });             
            }
             else {
                var n = noty({
                    text: 'Error. Please, contact with the administrator.',
                    type: 'error',
                    timeout: 3000
                });
             } 
             }
        });


    }

}