Javascript 从下拉列表中选择“购买”选项时,两个文本框需要获取设置的值

Javascript 从下拉列表中选择“购买”选项时,两个文本框需要获取设置的值,javascript,jquery,Javascript,Jquery,我有两个文本框封闭在一个文本框中。从上面的下拉列表中选择“购买”选项时,这两个框将隐藏。因为,我在这两个框中使用required,所以当选择Purchase选项时,它不会通过验证步骤。 问题:当从下拉列表中选择购买选项时,我如何设置两个文本框的值,即PropertyValue和ExistingLoanBalance <select id="selectPR" name="selectPR" required> <option value="">--Sele

我有两个文本框封闭在一个文本框中。从上面的下拉列表中选择“购买”选项时,这两个框将隐藏。因为,我在这两个框中使用required,所以当选择Purchase选项时,它不会通过验证步骤。 问题:当从下拉列表中选择购买选项时,我如何设置两个文本框的值,即PropertyValue和ExistingLoanBalance

<select id="selectPR" name="selectPR" required>
        <option value="">--Select--</option>
        <option value="Purchase" id="Purchase">Purchase</option>
        <option value="Refinance" id="Refinance">Refinance</option>
    </select>
    <div class="PR">
    <label for="PropertyValue" id="PropertyValue" >Existing Property Value*</label>
    <input type="text" id="PropertyValue" name="PropertyValue" class="PropertyValue" required>
    <label for="ExistingLoanBalance" id="ExistingLoanBalance">Existing Loan Balance*</label>
    <input type="text" id="ExistingLoanBalance" class="ExistingLoanBalance" name="ExistingLoanBalance" required>

    </div>

--挑选--
购买
再融资
现有财产价值*
现有贷款余额*

您可以通过以下jquery处理文本框的隐藏条件

$(“选择”).change(函数(){
var值=$(this.val();
如果(值=‘购买’){
$(“#PropertyValue,#ExistingLoanBalance”).css(“显示”、“无”);
}
否则{
$(“#PropertyValue,#ExistingLoanBalance”).css(“显示”、“块”);
$(“#现有贷款余额,#PropertyValue”).prop(“必需”,true);
}
});

--挑选--
购买
再融资
现有财产价值*
现有贷款余额*

首先,两个输入字段都有相同的“id”和标签,id应该不同

选择“购买”选项时,您不希望设置两个文本字段的值,而是不希望使用必需的属性验证它们。总之,应根据下拉列表中的选择切换所需属性

您可以使用选择框的onchange事件来完成此操作

<select id="selectPR" name="selectPR" required>
        <option value="">--Select--</option>
        <option value="Purchase" id="Purchase">Purchase</option>
        <option value="Refinance" id="Refinance">Refinance</option>
    </select>
    <div class="PR">
    <label for="PropertyValue" id="lblPropertyValue" >Existing Property Value*</label>
    <input type="text" id="PropertyValue" name="PropertyValue" class="PropertyValue" required/>
    <label for="ExistingLoanBalance" id="lblExistingLoanBalance">Existing Loan Balance*</label>
    <input type="text" id="ExistingLoanBalance" class="ExistingLoanBalance" name="ExistingLoanBalance" required/>
    </div>

<script type="text/javascript">
  $('#selectPR').on('change', function() {
    if( this.value == 'Purchase'){
       $('#PropertyValue').removeAttr('required');
       $('#ExistingLoanBalance').removeAttr('required');
    }
    else {
       $('#PropertyValue').attr('required', 'required');
       $('#ExistingLoanBalance').attr('required', 'required');
    }
  });
</script>

--挑选--
购买
再融资
现有财产价值*
现有贷款余额*
$('#selectPR')。在('change',function()上{
如果(this.value==“购买”){
$('PropertyValue')。removeAttr('required');
$('ExistingLoanBalance')。removeAttr('required');
}
否则{
$('PropertyValue').attr('required','required');
$('ExistingLoanBalance').attr('required','required');
}
});

注意:我已经更改了HTML中输入字段的Id,jquery代码中使用了相同的Id

首先,输入标记上的Id是相同的,我已经对此进行了更新,您应该修改它们(尽管我的解决方案中没有使用该Id)以适应您的偏好。 解决此问题的简单方法是,每当下拉列表的值等于“购买”时,更改这些输入框的显示属性,如下所示:

var-selectPR=document.getElementById(“selectPR”);
var prop=document.getElementById(“propvalue”);
var loan=document.getElementById(“existloanbalance”);
var PR=document.querySelector(“.PR”)
选择pr.onchange=function(){
如果(选择pr.value==“购买”){
PR.style.display=“无”
贷款移除属性(“必需”)
删除属性(“必需”)
}否则{
PR.style.display=“块”
loan.setAttribute(“必需”和“”)
prop.setAttribute(“必需”和“”)
}
}

--挑选--
购买
再融资
现有财产价值*
现有贷款余额*

您的问题是?@Rotimi当从下拉列表中选择购买选项时,我如何设置两个文本框的值?请解释问题和一个示例,以便更好地理解。@JustIn添加了问题,请让我知道是否清楚我使用了你的建议,包括笔记中的信息,但它似乎不起作用。在做了这些更改之后,当我从下拉菜单中选择“再融资”选项时,我没有看到两个文本框所需的验证!好的,等一下,我会在我的回答中添加一些额外的内容。如果之前提到,请手动删除hide和RequiredNow的所有属性。您能看到它吗?是的。非常感谢。我尝试了修改后的解决方案,当我选择“再融资”选项时,我没有看到两个文本框所需的验证。因为,我从两个文本框中删除了必需的属性,它通过了验证。选择“购买”时不会删除该属性。选择“购买”选项时会发生什么情况?您如何验证是否删除了“必需”属性?我在JSFIDLE中复制了相同的代码,它工作正常,更多的细节将有助于获得更好的建议。当选择购买选项时,当文本框隐藏时,它会通过验证吗?这可以通过使用remove attribute方法来完成,现在它应该通过了,因为我已经编辑了答案