简体   繁体   English

用jquery选择一个选项,并且在select元素中还有一个onchange()吗?

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

I'm trying to select VISA in this selection, but it won't work. 我正在尝试在此选择中选择VISA,但是它不起作用。 I've tried almost everything. 我已经尝试了几乎所有东西。

$('#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");

The problem is that the updateCreditCardType() function changes around the html file so I can obtain the CCV option. 问题是,updateCreditCardType()函数围绕html文件更改,因此我可以获得CCV选项。 It just isn't selecting. 它只是没有选择。

<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>

Just trigger the change event to force the HTML-bound onchange handler to fire. 只需触发change事件以强制触发绑定HTML的onchange处理函数即可。

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

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

use this to set the value once the document is ready 文档准备好后,用它来设置值

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

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

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

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