weixin_33698043 2012-11-30 22:17 采纳率: 0%
浏览 22

从ajax调用传递值

is there a work around on how I can pass the value from 'value' going to an if statement?

I get the value now showing 1 or 0. What im trying to accomplish now is how I can pass it in the if statement. I wanted to have something like this. If val = 0 return true; else return false. Im not sure I'm in the right track of using the ajax function. But if there a better way you can show me I will appreciate it.

$(function(){ 
       $('#add_product').click(function(){ 
         var i = $('#product_name').val(); 
         param = 'product_name='+i; 
         check_product(param).done(function(value) {
        var val = value; //value return is 1 or 0 - this is fine
         });
         //val should be past here so that I can determine if I will return true or false
         if(val == 0){
            return true;
         } else{
            return false;
         }
       }); 
    });

function check_product(param){ 
  return $.ajax({ 
    type : 'POST', 
    data : param, 
    url : baseurl+'cart/check_product_name/'
 });
}

html form:

<?php echo form_open_multipart('cart/save_product'); ?>
<h3>Product Name:</h3>
<?php echo form_input(array('name' => 'product_name', 'id' => 'product_name'), set_value('product_name')); ?>
<h3>Price:</h3>
<?php echo form_input(array('name' => 'price', 'id' => 'price'), set_value('price')); ?>
<h3>Status:</h3>
<?php
$options = array(
    '1' => 'Active',
    '0' => 'Not Active'
);
echo form_dropdown('status', $options);
 ?>
<h3>Short Description:</h3>
<?php echo form_textarea(array('name' => 'short_description', 'id' => 'short_desc'), set_value('short_description')); ?>
<h3>Image:</h3>
<?php echo form_upload('userfile'); ?><br>
<p class="submit" id="post_submit"><input type="submit" value="Submit" id="add_product" /></p>
</form>
  • 写回答

1条回答 默认 最新

  • weixin_33714884 2012-11-30 22:19
    关注

    You'll need to prevent the form from submitting, and then check with ajax if the product name exists, and when the ajax call has completed do the form submit with javascript if the returned value is zero:

    $(function() {
        $('#add_product').click(function(e) {
            e.preventDefault();
            var i     = $('#product_name').val(),
                param = 'product_name=' + i,
                self  = this;
            check_product(param).done(function(value) {
                if (value === 0) self.submit();
            });
        });
    });
    
    function check_product(param) {
        return $.ajax({
            type: 'POST',
            data: param,
            url: baseurl + 'cart/check_product_name/'
        });
    }​
    
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器