我在哪里添加JavaScript?

问题描述 投票:0回答:8

我得到了这个演示,但是我很困惑应该将javascript放在html中以使其工作。我使用的是崇高的文字,我不确定如何在浏览器中执行此操作。它在演示中效果很好,但是我无法在浏览器中使用它。我需要帮助使javascript和html在我的代码中一起工作。我尝试将其放在我的html代码之后,但是它不起作用,所有字段都显示了,下拉菜单没有任何作用。这是演示https://jsfiddle.net/5f3p2gzo/1/]的链接>


$('#cases').change(function () {
      var value = this.value;
    $('div').hide()
    $('#' + this.value).show();
});

我得到了这个演示,但是我很困惑应该将javascript放在html中以使其工作。我使用的是崇高的文字,我不确定如何在浏览器中执行此操作。在...

javascript html sublimetext demo
8个回答
0
投票

您需要在head标签下添加javascript文件,或如下所示:


0
投票
<html>
<head>
  <title>Multiplication Table</title>
  <style>
        body {
            padding: 5px;
        }

        label {
            font-weight: bold;
        }

        input[type=text] {
            width: 20em
        }

        p {
            margin: 1em 0 0;
        }

  </style>
  <script type="text/javascript">
        $('div').hide()

        $('#cases').change(function () {
              var value = this.value;
            $('div').hide()
            $('#' + this.value).show();
        });
  </script>
</head>
<body>
<select name="cases" id="cases">
        <option value="general">General Inquiry</option>
        <option value="credit">Credit Inquiry</option>
        <option value="payment">Payment Issue</option>
    </select><br>
    <label for="email">Email Address <span>*</span></label>
    <input type="text">
    <label for="full name">Full Name <span>*</span></label>
    <input type="text">


    <div class="general" id="general">
        <label for="documents">Wish to submit any requested documents?</label>
        <input type="radio" name="radio">Yes
        <input type="radio" name="radio">No <br><br>
        <label for="select">How did you find out about us?<span>*</span></label><br>
        <select name="case" id="case-type">
            <option value="value1">Value 1</option>
            <option value="value2">Value 2</option>
            <option value="value3">Value 3</option>
        </select><br>
    </div>

    <div class="credit" id="credit">
        <label for="Date of Inquiry">Date of Inquiry<span>*</span></label>
        <input type="date">
        <label for="Agency">Agency 3 <span>*</span></label>
        <input type="text">         
    </div>

    <div class="payment" id="payment">
        <label for="Service Phone Number">Service Phone Number<span>*</span></label>
        <input type="text">
        <label for="select">Topic<span>*</span></label><br>
        <select name="case" id="case-type">
            <option value="topic1">Topic 1</option>
            <option value="topic2">Topic 2</option>
            <option value="topic3">Topic 3</option>
        </select><br><br>
    </div>
    <br><br>
    <button>Submit</button>
</body>
</html>

0
投票

您可以将js代码放在html元素下的主体中的脚本标签中但您需要将jquery lib放在代码之上


0
投票

您需要将此代码放在<script>标记中,如下所示:


0
投票

在您的HTML中,您将在脚本标签中包含该代码。您还需要导入jQuery,这样代码才能真正起作用。


0
投票

您需要在关闭<body>之前在文件末尾添加类似的内容。


0
投票

将其放在html文件的末尾。


0
投票

有多个放置位置。

© www.soinside.com 2019 - 2024. All rights reserved.