forms - Javascript 根据选择选项更改 <p> 内容

标签 forms select javascript

不是最好的 Javascript 所以我想问我哪里错了。

as the title suggests, I have a select box with 4 different options, when an option is selected I want to change the contents of a <p> id 为 pricedesc 的标签。这是我目前所拥有的。

function priceText(sel)
{
    var listingType = document.getElementById('listingtype');
    var priceDesc = document.getElementById('pricedesc');
    if ( sel.options[sel.selectedIndex].value == "Residential Letting" ) {
    priceDesc = "Enter price per month";
    }
    else if ( sel.options[sel.selectedIndex].value == "Short Let" ) {
    priceDesc = "Enter price per week";
    }
    else if ( sel.options[sel.selectedIndex].value == "Serviced Accommodation" ) {
    priceDesc = "Enter price per week";
    }
    else if ( sel.options[sel.selectedIndex].value == "Sale" ) {
    priceDesc = "Enter for sale price";
    }

} 

在 body 里我有:

            <label>Listing Type:</label>
            <select name="listingtype" id="listingtype" onchange="priceText(this);">
                <option value="Residential Letting">Residential Letting</option>
                <option value="Short Let">Short Let</option>
                <option value="Serviced Accommodation">Serviced Accommodation</option>
                <option value="Sale">Sale</option>
            </select>


            <label>Price:</label>
            <p id="pricedesc">Enter price</p>
            <input name="price" type="text" id="price" value="<%=Request.Form("price")%>" maxlength="10" />

感谢您的帮助。

J.

最佳答案

更改设置段落内容的行

priceDesc = "Enter price per month";

priceDesc.innerHTML = "Enter price per month";

目前,您只是将 priceDesc 变量更改为包含字符串而不是段落节点。设置节点的 innerHTML 属性会更改其中包含的 html。 :D

关于forms - Javascript 根据选择选项更改 <p> 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3180710/

相关文章:

javascript - 如何停止提交并提醒用户用JS选择的问题少于10个问题

php - 提交后更改表单字段的数据

mysql - MySQL 中的自然连接与匹配列

ruby-on-rails - Rails 金钱 gem 和表单生成器

html - html形式的提交格式

sql-server - MSSQL 选择 "vertical"-where

jquery - 使用 jQuery 选择焦点文本在 Safari 和 Chrome 中不起作用

javascript - 将缓冲区(图像)转换为文件

javascript - 如何使用 WEB API 2 Controller 在 Angular.js 和 PostgreSQL 之间创建连接?

javascript - 在 jQuery Mobile 中完成 ajax 导航时执行 javascript 代码