为了账号安全,请及时绑定邮箱和手机立即绑定

访问一行中的单个列,默认情况下使用页面加载时的单选按钮选择

访问一行中的单个列,默认情况下使用页面加载时的单选按钮选择

拉莫斯之舞 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 关注
  • 95 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信