亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

訪問一行中的單個列,默認情況下使用頁面加載時的單選按鈕選擇

訪問一行中的單個列,默認情況下使用頁面加載時的單選按鈕選擇

拉莫斯之舞 2023-01-06 11:10:13
使用下面的代碼,我試圖從表中的一行訪問特定列“數量”。發生的情況是頁面加載時默認選擇其中一行,而用戶選擇時可以選擇其余行。我創建了一個點擊事件處理程序來處理手動選擇。使用類名訪問列時,它不返回任何內容。我需要將此值分配給相同形式的輸入框。我會附上該行的圖像表標記:        <tr valign="top" class="row6">            <td>            {if $tpl_order_details[lineitems].quantity > 1}                {if $radio_flag == "false"}                    <input type="radio" name="line_item" class="radio_class" id="line_item" value="{$tpl_order_details[lineitems].mSku}" checked onclick="handleClick(this);"/>                    {assign var=radio_flag value='true'}                {else}                    <input type="radio" name="line_item" class="radio_class" id="line_item" value="{$tpl_order_details[lineitems].mSku}" onclick="handleClick(this);" />                {/if}               {/if}            </td>               <td>            <a href="http://{$smarty.server.SERVER_NAME}/search/?q={$tpl_order_details[lineitems].sku}" target="_new">{$tpl_order_details[lineitems].sku}</a>            </td>            <td>&nbsp;            </td>            <td>{$tpl_order_details[lineitems].item_description}</td>            <td class="quantity_class" >{$tpl_order_details[lineitems].quantity}</td>            <td>{$tpl_order_details[lineitems].item_status}</td>使用循環外的輸入字段標記:<table><tr><td><label for="new_quantity">Enter New Quantity</label></td><td><input type="number" id="split_quantity" name="split_quantity"  min="1" max="6"></td><td><button type="submit" value="Save" name="submit_action">Submit</button></td><td><button type="submit" value="Cancel" name="submit_action">Cancel</button></td></tr></table>腳本:// This is to handle the radio button selected by default on page load.$( document ).ready(function() {    var firstRadioValue = 0;    firstRadioValue = $("input[name='line_item']:checked").val();    $('input[name="split_quantity"]').attr('max', firstRadioValue);    var quantity = $(".radio_class").parent().find(".quantity_class").val();    alert(quantity);});
查看完整描述

1 回答

?
慕桂英4014372

TA貢獻1871條經驗 獲得超13個贊

您在樹上走得還不夠遠,無法找到課程。你有:


var quantity = $(".radio_class").parent().find(".quantity_class").val();

這讓你到父<td>你正在尋找的元素是這個的兄弟:


<td class="quantity_class" >...

你想要做的是向上移動一個元素(表格行),然后從那里找到你正在尋找的類,所以使用closest(). 請注意,.quantity_class它沒有值,因此您必須在表格單元格中獲取文本:


var quantity = $(".radio_class").closest('tr').find(".quantity_class").text();

此外,我沒有看到任何帶有max屬性的標記或任何帶有名稱的標記split_quantity。


編輯- 根據與用戶的對話,發現需要進行一些更改。split_quantity首先,需要識別持有的表格,以便它可以在更宏大的標記中定位:


<table id="split_quantity_id">

    <tr>

        <td><label for="new_quantity">Enter New Quantity</label></td>

        <td><input type="number" id="split_quantity" name="split_quantity" min="1" max="6"></td>

        <td><button type="submit" value="Save" name="submit_action">Submit</button></td>

        <td><button type="submit" value="Cancel" name="submit_action">Cancel</button></td>

    </tr>

</table>

然后我們擺脫了onclick="handleClick(this)內聯 JavaScript,轉而讓 jQuery 處理點擊事件。最后我們重構了函數:


$(function() {

    var firstRadioValue = 0;

    firstRadioValue = $("input[name='line_item']:checked").closest('tr').find('.quantity_class').text();

    $('input[name="split_quantity"]').attr('max', firstRadioValue);

    var quantity = $(".radio_class").closest('tr').find(".quantity_class").text();

    console.log(quantity);


    $('table').delegate('.line_item', 'click', function(){

        currentRadioValue = $(this).closest('tr').find('.quantity_class').text();

        console.log(currentRadioValue);

        $('#split_quantity_id').find('[name="split_quantity"]').attr('max', currentRadioValue);

    });

});

注意:還發現 OP 使用的是 Smarty 2,它是使用舊版本 jQuery 的舊版本 Smarty,因此.delegate()使用 代替on().


查看完整回答
反對 回復 2023-01-06
  • 1 回答
  • 0 關注
  • 150 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號