Javascript 在ajax中始终选择else条件
我想给出一个条件,如果id=type_investor的值为1或6,则使用不同的控制器发送邮件 这是我的全部代码:Javascript 在ajax中始终选择else条件,javascript,jquery,ajax,Javascript,Jquery,Ajax,我想给出一个条件,如果id=type_investor的值为1或6,则使用不同的控制器发送邮件 这是我的全部代码: function (isConfirm) { if (!isConfirm) return; $.ajax({ url: "{{URL::to("cpanel/deposit-withdraw-list/approve-depositwithdraw")}}", type
function (isConfirm) {
if (!isConfirm) return;
$.ajax({
url: "{{URL::to("cpanel/deposit-withdraw-list/approve-depositwithdraw")}}",
type: "POST",
data: {id: fund_id,key: refdc_data},
dataType: "html",
success: function () {
$.ajax({
url: "{{URL::to("cpanel/deposit-withdraw-list/approve-tostatement")}}",
type: "POST",
data: {id: fund_id},
dataType: "html"});
if ($('#type_investor').val == 1){
$.ajax({
url: "{{URL::to("cpanel/deposit-withdraw-list/sendmail-deposit")}}",
type: "POST",
dataType: "html"});
}else{
$.ajax({
url: "{{URL::to("cpanel/deposit-withdraw-list/sendmail-withdraw")}}",
type: "POST",
dataType: "html"});
}
swal("Approved!", "It was succesfully Approved!", "success");
$('#refdc').val('');
window.location.href = "{{URL::to("cpanel/deposit-withdraw-list/")}}";
},
error: function (xhr, ajaxOptions, thrownError) {
swal("Error Approved!", "Please try again", "error");
window.location.href = "{{URL::to("cpanel/deposit-withdraw-list/")}}";
}
});
我想,也许问题的关键在于这段代码
if ($('#type_investor').val == 1){
$.ajax({
url: "{{URL::to("cpanel/deposit-withdraw-list/sendmail-deposit")}}",
type: "POST",
dataType: "html"});
}else{
$.ajax({
url: "{{URL::to("cpanel/deposit-withdraw-list/sendmail-withdraw")}}",
type: "POST",
dataType: "html"});
}
因为如果id=“type_investor”
的条件值为1
,则在else条件下使用控制器。
它总是使用其他条件。我不知道我的问题到底出在哪里。
val
是一个函数,您必须调用它:
注意,这里依赖于隐式转换,因为
val
总是返回一个字符串(除非jQuery集为空,否则它将返回undefined
)。FWIW,您可以在中找到将字符串隐式或显式转换为数字的各种选项。您正在调用的$(“#type_investor”).val
返回值未定义
val是函数而不是属性
你应该称之为
$('#type_investor').val();
我找到了解决方案,在使用调试器之后,我使用了.text()。代码是这样的
if ($('#type_investor').text == 1){
$.ajax({
url: "{{URL::to("cpanel/deposit-withdraw-list/sendmail-deposit")}}",
type: "POST",
dataType: "html"});
}else{
$.ajax({
url: "{{URL::to("cpanel/deposit-withdraw-list/sendmail-withdraw")}}",
type: "POST",
dataType: "html"});
}
试试
$('type_investor').val()
$('type_investor')。val
是一个永远不会出错的函数。您希望测试该函数的返回值。首先,$(“#type_investor').val()的值是多少?
?。尝试使用alert
或console.log
查看返回值。@TrafalgarDLaw:不要在黑暗中用alert
或console.log
火炬,用调试器打开灯。@T.J.Crowder是的,你也可以使用调试器。我添加了()类似的命令,但这不起作用。我还有别的condition@arbong:那么id=“type\u investor”
元素不存在,或者没有值“1”
。这个值存在并且有value=“1”@arbong如果它确实存在,那么它就是正确的。@arbong:不,它没有。如果是这样,$('#type_investor').val()==1
将是true
。您是否可以有多个id=“type\u investor”
元素?如果是这样,通常会选择第一个元素(尽管由于具有相同id的多个元素无效,这不保证,并且依赖于浏览器的底层行为)。如果id=“type_investor”的值为1,如果另一个值为6,那么……$(“#type_investor”).val()==1 | |$(“#type_investor”).val()==6
if ($('#type_investor').text == 1){
$.ajax({
url: "{{URL::to("cpanel/deposit-withdraw-list/sendmail-deposit")}}",
type: "POST",
dataType: "html"});
}else{
$.ajax({
url: "{{URL::to("cpanel/deposit-withdraw-list/sendmail-withdraw")}}",
type: "POST",
dataType: "html"});
}