使用jquery选择一个选项,并且在select元素中也有一个onchange()

Selecting a option with jquery and also has a onchange() in the select element?

本文关键字:元素 select onchange 有一个 选择 jquery 一个 使用 选项      更新时间:2023-09-26

我试图在这个选择中选择VISA,但它不起作用。我几乎什么都试过了。

$('#creditcard_cctype option[value="VISA"]').click();
$('#creditcard_cctype').val("VISA");
$('#creditcard_cctype option[value="VISA"]').prop("selected", true);
$('#creditcard_cctype option[value="VISA"]').attr("selected", true);
$('#creditcard_cctype option[value="VISA"]').cttr("selected", "selected");

问题是updateCreditCardType()函数在html文件周围发生了更改,因此我可以获得CCV选项。它只是没有选择。

<select name="creditcard_cctype" id="creditcard_cctype" onchange="updateCreditCardType()" class="Field250">
    <option value="">-- Please Choose --</option>
    <option id="CCType_AMEX" class=" requiresCVV2" value="AMEX">American Express</option>
    <option id="CCType_DISCOVER" class=" requiresCVV2" value="DISCOVER">Discover</option>
    <option id="CCType_MC" class=" requiresCVV2" value="MC">Mastercard</option>
    <option id="CCType_VISA" class=" requiresCVV2" value="VISA">Visa</option>
</select>

只需触发change事件,即可强制触发绑定HTML的onchange处理程序。

$('#creditcard_cctype').val('VISA').trigger('change');

jsFiddle演示:https://jsfiddle.net/c3sqhrma/

一旦文档准备好,就使用它来设置值

$('#creditcard_cctype').val('VISA');

http://jsfiddle.net/c3sqhrma/2/