【问题标题】:documet.getElementBy is is not passing thae valuedocument.getElementByid 没有传递值
【发布时间】:2018-10-09 05:14:01
【问题描述】:

我的 javascript 代码是:

    document.getElementById('district').value =  dist.long_name "this line is passing the value"
    document.getElementById('city').value =  route.long_name "this doesn't"

View the image of page

我的 HTML 是:

<div class="form-group">
    <label>District</label>
    <select id="district" class="form-control">
        <option id="district" selected='selected' style="display:none;"><label id="district"></label> </option>
        <option>Thrissur</option>
        <option>Kozhikode </option>
        <option>Palakkad </option>
    </select>
</div>

<div class="form-group">
    <label>City</label>

    <select id="city" class="form-control"> 
        <option id="city" selected='selected' style="display:none;"><label id="city"></label> </option>   
        <option>opt1</option>
        <option>opt2 </option>
        <option>opt3 </option>
    </select>
</div>

我收到以下错误,我该如何解决:

documet.getElementBy 没有传递值

【问题讨论】:

  • city 是一个没有.value 属性的&lt;label&gt;。 (啊,你使用了 3 次 id city - 这是不允许的)

标签: javascript html


【解决方案1】:

您应该在 HTML 中拥有唯一的 ID。

 <select id="city" class="form-control"> 

<label id="city"></label>

不可接受。

【讨论】:

  • 包含 HTML .. 的选项元素怎么样?
  • 整个 HTML 文档中的 id 不应该相同。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-02
  • 1970-01-01
  • 2020-10-13
  • 1970-01-01
  • 2020-01-09
  • 2013-02-06
相关资源
最近更新 更多