HTML:

<div class="col-md-6 parameter">
    <h3>Property Type</h3><br>
    <input type="radio" value="residential" name="property_type" id="residential-property"><label for="residential-property">Residential</label><br>
    <input type="radio" value="commercial" name="property_type" id="commercial-property"><label for="commercial-property">Commercial</label><br>
</div>


JS:

// Get Property Type
var property_type = document.querySelector('[name=property_type]:checked').value || "";
console.log(property_type);

if (property_type = "residential") {
    hello.innerHTML = "<div class='answer'>" + property_type + "</div>";
}


console.log(property_type)正确识别选中的单选按钮,并返回“ residential”或“ commercial”值。但是,当我尝试使用此选定值时,它始终默认为“ residential”。

例如,如果我选择“商业”单选按钮并单击“提交”,则“ hello.innerHTML”中的“ property_type”值始终默认为“ residential”。奇怪的是,如果我从“ if”循环中取出“ property_type”值,则每当我提交时,它都会根据我选中的单选按钮返回正确的值,无论是“住宅”还是“商业”。

任何帮助表示赞赏!

最佳答案

是平等的,

您需要在if(...){}中使用==代替=

if( property_type == "residential")

07-24 09:43
查看更多