我有jQuery代码,我试图将一个类值放在一个变量&然后尝试在if else条件下使用该变量值&与静态值比较FCL'但那不起作用。类名是' kuuj' &安培;此类值可以是(LCL,FCL,BULK,AIR)。
var xxssees = $('.kuuj').val();
if(xxssees == "FCL")
{
$('#sonal').html(xxssees);
}
else
{
$('#sonal').html('hello');
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<strong>Solution : </strong><label id="sonal" style="color:Green"></label><br />
&#13;
答案 0 :(得分:2)
1。val() is only applicable for an input element
然后您的代码将正常运行。检查如下: -
$(document).ready(function(){
var xxssees = $('.kuuj').val();
if(xxssees == "FCL"){
$('#sonal').text(xxssees);
}else {
$('#sonal').text('hello');
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<strong>Solution : </strong><label id="sonal" style="color:Green"></label><br />
<input class="kuuj" value = "FCLs">
2.如果kuju
类在输入之外的任何其他元素上,则使用.text()
或.html()
来获取值。检查以下内容: -
$(document).ready(function(){
var xxssees = $('.kuuj').html(); // you can use .text() also
if(xxssees == "FCL"){
$('#sonal').html(xxssees); //you can use .text() also
}else {
$('#sonal').html('hello'); //you can use .text() also
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<strong>Solution : </strong><label id="sonal" style="color:Green"></label><br />
<div class="kuuj">FCLs</div>
答案 1 :(得分:0)
您需要添加输入字段而不是标签
而不是
<label id="sonal" style="color:Green"></label>
您添加
<input type="text" id="sonal" value="" />
随时询问是否需要进一步的帮助
答案 2 :(得分:0)
$(document).ready(function(){
var xxssees = $('.kuuj').val();
console.log(xxssees);
if(xxssees == "FCL"){
$('#sonal').html(xxssees);
}else {
$('#sonal').html('hello');
}
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<strong>Solution : </strong><label id="sonal" style="color:Green"></label><br />
<input type="hidden" class="kuuj" value = "FCL">
&#13;