Jquery没有专注于正确的字段


Jquery not focusing on the correct fields

在jquery中运行计算后,我试图将焦点放在我的isbn文本框或版本文本框中,但它转到页面上的第一个按钮或销售排名字段。

我的网址是:

    <td><label>ISBN</label></td>
                <td><input type="text" id="isbn" name="isbn" /></td>
                <td></td>
                <td rowspan=7><a id="detailURL" href="#" target="_blank"><img id="bookCover" src="../images/imgNotFound.gif" height="200px" /></a></td>
                <td rowspan=7><input type="button" id="isIE" name="isIE" value="IE Price Reduction" />
                    <br/><br/><input type="button" id="isAIE" name="isAIE" value="AIE Price Reduction" />
                    <br/><br/><input type="button" id="isModHighlight" name="isModHighlight" value="Moderate Highlighting" />
                    <br/><br/><input type="button" id="isHeavHighlight" name="isHeavHighlight" value="Heavy Highlighting" />
                    <br/><br/><input type="button" id="isMissingSupply" name="isMissingSupply" value="Missing Supply" />
                    <br /><br/><input type="button" id="waterDamage" name="waterDamage" value="Water Damage / Poor Cond." />
                </td>
                <td rowspan=7>
                    <!-- SUPPLY CHECK TABLE -->
                    <table id="supply" summary="Check Supply" width="10%" border="1" align="right">
                    <caption><h3>Check Supply!</h3></caption>
                    <thead>
                    <tr>
                        <th scope="col" class="rounded-isbn">Supply:</th>
                        <!--<th scope="col" class="rounded-isbn">Who Requires:</th>-->
                    </tr>
                    </thead>
                    <tbody>
                    </tbody>
                    </table>
                </td>
            </tr>
            <tr>
                <td><label for="quantity" class="label">Quantity</label></td>
                <td><input type="text" id="quantity" name="quantity" /></td>
                <td></td>
            </tr>
            <tr>
                <td><label for="payPrice" class="label">Price to Pay</label></td>
                <td><input type="text" id="payPrice" name="payPrice"/></td>
                <td></td>
            </tr>
            <tr>
                <td><label>Edition</label></td>
                <td><input type="text" id="edition" name="edition" readonly="readonly"/></td>
                <td></td>
            </tr>
            <tr>
                <td><label>Title</label></td>
                <td><input type="text" id="title" name="title" /></td>
                <td></td>
            </tr>
            <tr>
                <td><label>Sales Rank</label></td>
                <td><input type="text" id="salesRank" name="salesRank" readonly="readonly" /></td>
                <td></td>
            </tr>

            <tr>
                <td></td><td><input type="button" id="buy" value="Buy" /></td><td></td>
    </table>
    </form>

有问题的jquery是:

if($('#payPrice').val() <= 0 ) {
    $(':text')[0].focus();
} else {
    $("#edition").focus();
}

#payPrice 的值为 <=0 时,它默认为第一个按钮id="isIE"而不是isbn字段。如果#payprice的值>0则它侧重于销售排名文本区域,而不是版本。 我尝试设置超时,但效果不如$("#isbn).focus();,这也不起作用。 我将其更改为$(':text')[0].focus();以查看是否有效,但它仍然转到第一个按钮。我在这里做错了什么? 我如何纠正它以使其正常工作?

你试过吗

if(parseFloat($('#payPrice').val()) <= 0 ) {
...
}

如果 parseFloat 没有区别,请尝试以下操作:

if($('#payPrice').val()<=0){
   $("input[name='isbn']").focus(); 
}else{
   $("input[name='edition']").focus(); 
}

否则,您可以看到该示例:

function sampledFunction()
{
 window.setTimeout(function ()
 {
  $('#mobileno').focus();
 }, 0);
 return false;
}

在这里阅读

我建议使用更好的选择器:

喜欢$('[type=text]')$('input:text')

参见:jQuery文本选择器

这是最终的结果,感谢JackTurky为我指出了正确的方向。

    //focus on either isbn or edition
        setTimeout(function() {
            if($('#payPrice').val() <= 0 ) {
                    $("#isbn").focus();
                } else {
                    $("#edition").focus();
                }
                }, 10);