不是最好的 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/