最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - String is not defined in JavaScript function? - Stack Overflow

programmeradmin2浏览0评论

I have a dropdown box with an onchange event. When I select a value in the dropdown I invoke a JavaScript function that accepts a few parameters then submits values to my controller. Here's what I have for the dropdown box:

        <td><select class="input-medium" name="status" id="status" onchange="javascript:changeStatus(this.value, ${module.id}, ${module.status});">
                        <option <c:if test='${module.status == "Active"}'>selected="selected"</c:if> value="active">Active</option>
                        <option <c:if test='${module.status == "Inactive"}'>selected="selected"</c:if> value="inactive">Inactive</option>
                        <option <c:if test='${module.status == "Retired"}'>selected="selected"</c:if> value="retired">Retired</option>
                        </select></td>

Here's my JavaScript function:

     <script type="text/javascript">
        function changeStatus(dropboxStat, modID, curStat) { 
            alert (dropboxStat);
            if (curStat.toString == "Active" && dropboxStat == "inactive")
                alert ("This will delete all of the module's training entries that have not been pleted!");

            document.updateForm.status.value = dropboxStat;
            document.updateForm.modID.value = modID;
            document.updateForm.submit();
            } 
     </script>

When I change a row that had a status of "active" to "inactive", the if statement should be read as true and the alert should pop up. Right now I'm not even getting the first alert to pop up. Instead, I get this error:

   ReferenceError: Active is not defined

I've read online about variables not being defined but this is just a String value...in which I thought wouldn't need to be defined. Any ideas and explanations as to why this error would get thrown?

I have a dropdown box with an onchange event. When I select a value in the dropdown I invoke a JavaScript function that accepts a few parameters then submits values to my controller. Here's what I have for the dropdown box:

        <td><select class="input-medium" name="status" id="status" onchange="javascript:changeStatus(this.value, ${module.id}, ${module.status});">
                        <option <c:if test='${module.status == "Active"}'>selected="selected"</c:if> value="active">Active</option>
                        <option <c:if test='${module.status == "Inactive"}'>selected="selected"</c:if> value="inactive">Inactive</option>
                        <option <c:if test='${module.status == "Retired"}'>selected="selected"</c:if> value="retired">Retired</option>
                        </select></td>

Here's my JavaScript function:

     <script type="text/javascript">
        function changeStatus(dropboxStat, modID, curStat) { 
            alert (dropboxStat);
            if (curStat.toString == "Active" && dropboxStat == "inactive")
                alert ("This will delete all of the module's training entries that have not been pleted!");

            document.updateForm.status.value = dropboxStat;
            document.updateForm.modID.value = modID;
            document.updateForm.submit();
            } 
     </script>

When I change a row that had a status of "active" to "inactive", the if statement should be read as true and the alert should pop up. Right now I'm not even getting the first alert to pop up. Instead, I get this error:

   ReferenceError: Active is not defined

I've read online about variables not being defined but this is just a String value...in which I thought wouldn't need to be defined. Any ideas and explanations as to why this error would get thrown?

Share Improve this question asked Feb 11, 2015 at 14:47 user1750824user1750824 1
  • 2 curStat.toString == "Active" pares a function to a string. You probably meant to write curStat.toString() == "Active" – blgt Commented Feb 11, 2015 at 14:54
Add a ment  | 

1 Answer 1

Reset to default 6

Your onchange is being rendered as

changeStatus(this.value, 123, Active)

so it is seeing Active as a variable and not a string. Hence the error.

You need to add the missing quotes around your arguments

<td><select class="input-medium" name="status" id="status" onchange="javascript:changeStatus(this.value, '${module.id}', '${module.status}');">
发布评论

评论列表(0)

  1. 暂无评论