简体   繁体   English

如何使用 Javascript 设置默认下拉值?

[英]How to set a default dropdown value using Javascript?

I am trying to display forms based on the dropdown selected.我正在尝试根据所选的下拉列表显示 forms。 But, by default, I am able to see all the forms listed one below other.但是,默认情况下,我能够看到所有 forms 都列在另一个下方。 Only if I click on the particular dropdown, I am getting the dropdown specific forms. I have set display none to all the dropdown fields using CSS. Don't know where the problem is arising.只有当我点击特定的下拉菜单时,我才会得到特定的下拉菜单 forms。我已经使用 CSS 将所有下拉字段设置为不显示。不知道问题出在哪里。 What am I missing out?我错过了什么? Iam quite a beginner & just in learning stage so any help would be much appreciated.我是一个初学者,刚刚处于学习阶段,所以任何帮助将不胜感激。

The form:表格:

 <div className="offcanvas-body"> <small className="card-text mb-3 text-white"> Choose an option below to perform a transaction </small> <select name="select-element" className="form-control my-3" id="type-select"> <option value ="none">-- Select Transaction Type --</option> <option value="payment">Payment</option> <option value="transfer">Transfer</option> <option value="deposit">Deposit</option> </select> {/* <:-- Card: Payment Card --> */} <div className="card payment-card"> <div className="card-body"> <div className="form-group mb-2"> <label htmlFor=""> Account Holder / Beneficiary</label> <input type="text" name="beneficiary" className="form-control" placeholder="Enter Account holder / Beneficiary name" /> </div> <div className="form-group mb-2"> <label htmlFor=""> Beneficiary Account Number</label> <input type="text" name="account_number" className="form-control" placeholder="Enter Beneficiary Account No" /> </div> <div className="form-group "> <label htmlFor="">Select Acc ``` ount</label> <select name="account_id" className="form-control" id="transact-type"> <option value="">-- Select Account --</option> </select> </div> <div className="form-group mb-2"> <label htmlFor="">Reference</label> <input type="text" name="reference" className="form-control" placeholder="Enter Reference" /> </div> <div className="form-group mb-2"> <label htmlFor="">Enter Payment Amount</label> <input type="text" name="payment_amount" className="form-control" placeholder="Enter Payment Amount" /> </div> <div className="form-group mb-2"> <button id="" className="btn-md transact-btn">Pay</button> </div> </div> </div> {/* Transfer Card */} <div className="card transfer-card"> <div className="card-body"> <div className="form-group "> <label htmlFor="">Select Account</label> <select h="transfer_from" className="form-control" id=""> <option value="">-- Select Account --</option> </select> </div> <div className="form-group "> <label htmlFor="">Select Account</label> <select name="transfer_to" className="form-control" id=""> <option value="">-- Select Account --</option> </select> </div> <div className="form-group mb-2"> <label htmlFor="">Enter Transfer Amount</label> <input type="text" name="transfer_amount" className="form-control" placeholder="Enter Transfer Amount" /> </div> <div className="form-group my-2"> <button id="" className="btn-md transact-btn">Transfer</button> </div> </div> </div> {/* Deposit card */} <div className="card deposit-card"> <div className="card-body"> <form action="" className="deposit-form"> <div className="form-group mb-2"> <label htmlFor="">Enter Deposit Amount</label> <input type="text" name="deposit_amount" className="form-control" placeholder="Enter Deposit Amount" /> </div> <div className="form-group "> <label htmlFor="">Select Account</label> <select name="account_id" className="form-control" id="transact-type"> <option value="">-- Select Account --</option> </select> </div> <div className="form-group my-2"> <button id="" className="btn-md transact-btn">Deposit</button> </div> </form> </div> </div> </div>



**CSS :** 

.payment-card{
    display: none ;
}

.transfer-card{
    display: none ;
}

.deposit-card{
    display: none ;
}


JS : 


// Get Transaction Type:
const typeSelect = document.getElementsByName("select-element")[0];
console.log(typeSelect);

// Get Transaction Forms:

const paymentCard = document.querySelector(".payment-card");
const transferCard = document.querySelector(".transfer-card");
const depositCard = document.querySelector(".deposit-card");

// Check For Transaction Type Event Listener:
typeSelect.addEventListener('change', () => {

       if (typeSelect.value == "none") {
              paymentCard.style.display = "none";
              transferCard.style.display = "none";
              depositCard.style.display = "none";
       }

       else if (typeSelect.value == "payment") {
              paymentCard.style.display = "block";
              transferCard.style.display = "none";
              depositCard.style.display = "none";
       }
       else if (typeSelect.value == "transfer") {
              transferCard.style.display = "block";
              paymentCard.style.display = "none";
              depositCard.style.display = "none";
       }
       else if (typeSelect.value == "deposit") {
              depositCard.style.display = "block";
              paymentCard.style.display = "none";
              transferCard.style.display = "none";
       }



});
// End Of Check For Transaction Type Evenet Listener.

I am not gonna lie, my solution may be a bit hacky but I usually just set a drop down sign that says "[chose option]" or something similar where I save it to a variable of appropriate name.我不会撒谎,我的解决方案可能有点老套,但我通常只是设置一个下拉符号,上面写着“[选择选项]”或类似的东西,我将它保存到一个适当名称的变量中。 If they needed a value from it, I just use an if statement to tell them that they need to chose something.如果他们需要它的价值,我只是用一个 if 语句告诉他们他们需要选择一些东西。

On the other hand, if you want the value to already be there, have that default value to be your first option and just get elementById.value within javascript outside a function that changes on whatever you select on the dropdown menu.另一方面,如果您希望该值已经存在,则将该默认值作为您的第一个选项,只需在 function 之外的 javascript 内获取 elementById.value,无论您在下拉菜单上的 select 发生什么变化。

If you want the js to know that the dropdown has had the value change, on the same tag put如果你想让 js 知道下拉菜单的值发生了变化,在同一个标签上放

<select name="select-element" className="form-control my-3" id="type-select" onchange="myFunctionReactsOnChange()">

^Notice I added "onchange" attribute which leads to a function that includes the "()" at the end. ^注意我添加了“onchange”属性,它导致 function 最后包含“()”。

Hope it helped.希望它有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM