Javascript 根据选择选项更改

内容

2024-05-21

Javascript 不是最好的,所以我想问一下我哪里出了问题。

正如标题所示,我有一个带有 4 个不同选项的选择框,当选择一个选项时我想更改<p>带有priceesc id 的标签。这是我到目前为止所拥有的。

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";
    }

} 

在我的身体里有:

            <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";

to

priceDesc.innerHTML = "Enter price per month";

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Javascript 根据选择选项更改

内容 的相关文章