简体   繁体   English

从TD获取价值

[英]Get value from td

I print results from db. 我从数据库打印结果。 I am facing problem when I want to choose value from row. 我想从行中选择值时遇到问题。 I was trying to pick up the value by entering button (last column) on value, and insert that value in local storage. 我试图通过在值上输入按钮(最后一列)来获取值,然后将该值插入本地存储中。

<table class="table table-bordered">
    <thead>
        <tr class="bg-grey">
            <th>Broj </th>
            <th>Boja </th>
            <th>Količina</th>
            <th><center><i class="icon-cart"></i></center></th>
        </tr>
    </thead>
    <tbody>
    <?php  
        while ($r=$m->fetch()) {
            $id_print = $r['id']; 
            $boja = $r['Boja'];
            $kolicina = $r['Kolicina'];
            //  var_dump($id_print);
            ?>
        <tr>
            <td><?php echo "R - " . $id_print;?></td>
            <td><?php echo $boja;?></td>
            <td><?php echo $kolicina;?></td>
            <td><button  id= "item" value='<?php echo $id_print;?>' onclick="save()" class="ion-ios-cart-outline"></button></td>
        </tr>
        <?php  }  ?>    
    </tbody>
</table>

I am using function to get value from td. 我正在使用函数从td获取价值。 But I always get empty var. 但是我总是空var。

<script type="text/javascript">
    function save() {
        var items= document.getElementById('item').innerHTML;           

        localStorage.setItem('action', items);
    }
</script>

I am not doing something good, If someone can tell me what to change in order to get results. 我做的不好,如果有人可以告诉我要做什么才能取得结果。

If your goal is to save the value of the button that was clicked, you don't need any id s at all. 如果您的目标是保存所单击按钮的值,则根本不需要任何id

The minimum-changes approach is to pass this into your handler as an argument, and then use the argument's value property in the handler's code: 最小变化方法是将this作为参数传递给您的处理程序,然后在处理程序的代码中使用参数的value属性:

<td><button value='<?php echo $id_print;?>' onclick="save(this)" class="ion-ios-cart-outline"></button></td>
<!-- Note ------------------------------------------------^^^^^  -->

then 然后

function save(element) {
    localStorage.setItem('action', element.value);
}

You might also consider adding type="button" to your button elements, since the default type of button s is (to me, surprisingly) type="submit" , so if you have those buttons in a form, they'll submit the form. 您还可以考虑在button元素中添加type="button" ,因为button s的默认type是(对我来说很令人惊讶) type="submit" ,因此,如果您在表单中有这些按钮,它们将提交形成。

Re your comment: 发表您的评论:

That is exactly what I was looking for, but in table I have more rows and more could be selected. 那正是我想要的,但是在表中我有更多的行,可以选择更多的行。 By doing this only one value is possible to select. 这样做只能选择一个值。 Is it possible so save values in local storage by clicking on them 是否可以通过单击将值保存在本地存储中

If you mean as an array, yes, you can do that. 如果您要表示为数组,是的,您可以这样做。 Here's one way: 这是一种方法:

function save(element) {
    var actions = JSON.parse(localStorage.getItem("actions") || "[]");
    if (actions.findIndex(element.value) == -1) {
        actions.push(element.value);
        localStorage.setItem("actions", JSON.stringify(actions));
    }
}

That maintains an array in local storage as JSON (since all local storage values are strings). 这样会将数组在本地存储中维护为JSON(因为所有本地存储值都是字符串)。 The first part gets the existing array (if any) or a blank one (if none): 第一部分获取现有数组(如果有)或空白数组(如果没有):

var actions = JSON.parse(localStorage.getItem("actions") || "[]");

Then we use the ES2015 (aka "ES6") function Array#findIndex (which can be polyfilled/shimmmed, see MDN ) to see if the value is already in the array and, if not, we add it: 然后,我们使用ES2015(又名“ ES6”)函数Array#findIndex (可以进行填充/填充,请参见MDN )来查看该值是否已在数组中,如果没有,则添加该值:

if (actions.findIndex(element.value) == -1) {
    actions.push(element.value);
    localStorage.setItem("actions", JSON.stringify(actions));
}

If for any reason you don't want to shim/polyfill Array#findIndex , you can use the ES5 function Array#some instead: 如果出于任何原因您不想填充/填充Array#findIndex ,则可以改用ES5函数Array#some

if (!actions.some(function(e) { return e === element.value; })) {
    actions.push(element.value);
    localStorage.setItem("actions", JSON.stringify(actions));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM