[英]Jquery and Drop Down - Determine the Values
我有一個下拉菜單,需要比較下拉菜單中的值( 余額 )並將其與全局變量進行比較,然后執行if / else語句以顯示一些HTML。
這是下拉列表:
<select name="batch">
<option value='null'>-- None --</option>
<option value='96'>Check (#2200) (Bal. $84.00) - Jim Jones</option>
<option value='98'>Credit Card (#0) (Bal. $90.00) - Bailey Adams</option>
</select>
這是我的jQuery:
$("select[name=batch]").change(function () {
if ($(this).val() >= GlobalTotal) {
$("th#show_refund").show();
$("th#no_show_refund").hide();
} else {
$("th#show_refund").hide();
$("th#no_show_refund").show();
}
});
jQuery是否有可能確定HTML select內部的平衡? 如果是這樣,我很高興有一個指導讓我朝正確的方向前進。
我假設您正在動態構建它。 在每個選項上,添加一個屬性data-balance=84
或任何余額,然后使用jQuery,您將使用
$("select[name=batch] option:selected").attr("data-balance")
因此,您的完整代碼將是:
<select name="batch">
<option value='null'>-- None --</option>
<option value='96' data-balance="84.00">Check (#2200) (Bal. $84.00) - Jim Jones</option>
<option value='98' data-balance="90.00">Credit Card (#0) (Bal. $90.00) - Bailey Adams</option>
</select>
$("select[name=batch]").change(function () {
if ($("select[name=batch] option:selected").attr("data-balance") >= GlobalTotal) {
$("th#show_refund").show();
$("th#no_show_refund").hide();
} else {
$("th#show_refund").hide();
$("th#no_show_refund").show();
}
});
您可以從所選值的innerHTML
中提取該值:
// Store our Global Total
var gTotal = 87;
// Bind to the change event of our dropdown
$("select[name='batch']").on("change", function(){
// If we find a dollar amount in our selected element, save it to `amount`
if ( amount = $(":selected", this).html().match( /\$(\d+\.\d{2})/ ) )
// If that amount is greater than the Global Total
amount[1] >= gTotal
// Do something when it's greater than
? console.log( amount[1] + ' is greater than ' + gTotal )
// Do something else when it's lesser than
: console.log( amount[1] + ' is less than ' + gTotal ) ;
});
最好將選擇選項的值設置為余額,如下所示:
<select name="batch">
<option value='null'>-- None --</option>
<option value='84.00'>Check (#2200) (Bal. $84.00) - Jim Jones</option>
<option value='90.00'>Credit Card (#0) (Bal. $90.00) - Bailey Adams</option>
</select>
這使您無需進行任何時髦的文本分析即可獲得價值。 例如:
$("select[name=batch]").change(function () {
if ($("select[name=batch] option:selected").val() >= GlobalTotal) {
$("th#show_refund").show();
$("th#no_show_refund").hide();
} else {
$("th#show_refund").hide();
$("th#no_show_refund").show();
}
$("select[name=batch]").change(function () {
var selectedText= "";
$("select option:selected").each(function () {
selectedText+= $(this).text() + " ";
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.