douyi3760 2013-07-03 02:24
浏览 63
已采纳

JQuery:使用.val()函数不会更改下拉值

I encounter a very strange issue here.

I have two select fields that will output show same result if one of them changes selected value.

Here's the code:

    var qlt,internalQ;

    if (jQuery("#qlt").length>0){
                qlt =       (jQuery("#qlt").val()).split(":")[0];   
                qltDesc =   (jQuery("#qlt").val()).split(":")[1]; 
            }

    if (jQuery("#internalQ").length>0){
                internalQ = (jQuery("#internalQ").val()).split(":")[0];     
                qltDesc =   (jQuery("#internalQ").val()).split(":")[1]; 
            }

    <?php if(is_single('booklets')) { ?> 
    jQuery('#qlt').change(function(){ 
        if  (qlt=="4-1")                {   jQuery('#internalQ').val('4-1:Matt 90gsm');         }
            else if(qlt=="4-2")         {   jQuery('#internalQ').val('4-2:Silk/matt 128gsm');   }
            else if(qlt=="4-3")         {   jQuery('#internalQ').val('4-3:Silk/Matt 150gsm');   }
            calculate();
        }); 

        jQuery('#internalQ').change(function(){ 
            if  (internalQ=="4-1")      {   jQuery('#qlt').val('4-1:Matt 90gsm');               }
            else if(internalQ=="4-2")   {   jQuery('#qlt').val('4-2:Silk/matt 128gsm');         }
            else if(internalQ=="4-3")   {   jQuery('#qlt').val('4-3:Silk/Matt 150gsm');         }
            calculate();
        }); 
    <?php } ?>

The problem is the last conditions of each JQuery.change function (else if(internalQ=="4-3" and else if(qlt=="4-3")) does not work as expected. The value will change back to the first option, NOt the third.

If my description is not clear, my current page is: http://210.48.94.218/~printabl/products/booklets/

The fields I'm talking about are Cover Quality and Internal Quality

Q: Did I miss something here? Can you point it out? Any help will be greatly appreciated.

  • 写回答

3条回答 默认 最新

  • dqzow3859 2013-07-03 05:17
    关注

    Guys I already solved the issue. Its my bad actually.

    I have a HTML code as default that i used in displaying Cover Quality and Internal Quality:

    <tr>                                                                                 
        <td><?php echo"Cover Quality";?></td>   
        <td>
                     <select id="qlt" name="quality">
            <option value="4-1:Matt 90gsm">Matt 90gsm</option>
            <option value="4-2:Silk/matt 128gsm">Silk/Matt 128gsm</option>
            <option value="4-3:Silk/matt 150gsm">Silk/Matt 150gsm</option>
             </select></td>     
    </tr>
    

    And I used the jQuery($element).val('$val') to change the default value.

    What I'm doing wrong is: in third option in 'Cover and Interal Quality`

    else if (internalQ=="4-3") {jQuery('#qlt').val('4-3:Silk/Matt 150gsm'); }

    instead of:

    else if (internalQ=="4-3") {jQuery('#qlt').val('4-3:Silk/matt 150gsm'); }

    The difference is the Capital "M" and Small "m" in the values.

    So I changed, the third value in my jQuery Code(or you can change the HTML) so that the value will match exactly like what is in the HTML. Solved! :)

    Thanks for the prompt stackoverflow pros who helped and gave some idea. Now I know whats the cause of this issue.

    I will change the title so that programmers with the same problem will be directed to this post.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题